Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve header printing in json format for package glog; add golang v1.18 support for ci workflow #2037

Merged
merged 15 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/gf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:

strategy:
matrix:
go-version: [ "1.15", "1.16", "1.17" ]
go-version: [ "1.15", "1.16", "1.17", "1.18" ]
goarch: [ "386", "amd64" ]

steps:
Expand Down
2 changes: 1 addition & 1 deletion net/ghttp/ghttp_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (r *Request) SetError(err error) {

// ReloadParam is used for modifying request parameter.
// Sometimes, we want to modify request parameters through middleware, but directly modifying Request.Body
// is invalid, so it clears the parsed* marks to make the parameters re-parsed.
// is invalid, so it clears the parsed* marks of Request to make the parameters reparsed.
func (r *Request) ReloadParam() {
r.parsedBody = false
r.parsedForm = false
Expand Down
83 changes: 41 additions & 42 deletions os/glog/glog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,56 +136,55 @@ func (l *Logger) print(ctx context.Context, level int, stack string, values ...i
}
input.handlers = append(input.handlers, defaultPrintHandler)

if l.config.HeaderPrint {
// Time.
timeFormat := ""
if l.config.Flags&F_TIME_DATE > 0 {
timeFormat += "2006-01-02"
// Time.
timeFormat := ""
if l.config.Flags&F_TIME_DATE > 0 {
timeFormat += "2006-01-02"
}
if l.config.Flags&F_TIME_TIME > 0 {
if timeFormat != "" {
timeFormat += " "
}
if l.config.Flags&F_TIME_TIME > 0 {
if timeFormat != "" {
timeFormat += " "
}
timeFormat += "15:04:05"
timeFormat += "15:04:05"
}
if l.config.Flags&F_TIME_MILLI > 0 {
if timeFormat != "" {
timeFormat += " "
}
if l.config.Flags&F_TIME_MILLI > 0 {
if timeFormat != "" {
timeFormat += " "
timeFormat += "15:04:05.000"
}
if len(timeFormat) > 0 {
input.TimeFormat = now.Format(timeFormat)
}

// Level string.
input.LevelFormat = l.GetLevelPrefix(level)

// Caller path and Fn name.
if l.config.Flags&(F_FILE_LONG|F_FILE_SHORT|F_CALLER_FN) > 0 {
callerFnName, path, line := gdebug.CallerWithFilter(
[]string{utils.StackFilterKeyForGoFrame},
l.config.StSkip,
)
if l.config.Flags&F_CALLER_FN > 0 {
if len(callerFnName) > 2 {
input.CallerFunc = fmt.Sprintf(`[%s]`, callerFnName)
}
timeFormat += "15:04:05.000"
}
if len(timeFormat) > 0 {
input.TimeFormat = now.Format(timeFormat)
}

// Level string.
input.LevelFormat = l.GetLevelPrefix(level)

// Caller path and Fn name.
if l.config.Flags&(F_FILE_LONG|F_FILE_SHORT|F_CALLER_FN) > 0 {
callerFnName, path, line := gdebug.CallerWithFilter(
[]string{utils.StackFilterKeyForGoFrame},
l.config.StSkip,
)
if l.config.Flags&F_CALLER_FN > 0 {
if len(callerFnName) > 2 {
input.CallerFunc = fmt.Sprintf(`[%s]`, callerFnName)
}
if line >= 0 && len(path) > 1 {
if l.config.Flags&F_FILE_LONG > 0 {
input.CallerPath = fmt.Sprintf(`%s:%d:`, path, line)
}
if line >= 0 && len(path) > 1 {
if l.config.Flags&F_FILE_LONG > 0 {
input.CallerPath = fmt.Sprintf(`%s:%d:`, path, line)
}
if l.config.Flags&F_FILE_SHORT > 0 {
input.CallerPath = fmt.Sprintf(`%s:%d:`, gfile.Basename(path), line)
}
if l.config.Flags&F_FILE_SHORT > 0 {
input.CallerPath = fmt.Sprintf(`%s:%d:`, gfile.Basename(path), line)
}
}
// Prefix.
if len(l.config.Prefix) > 0 {
input.Prefix = l.config.Prefix
}
}
// Prefix.
if len(l.config.Prefix) > 0 {
input.Prefix = l.config.Prefix
}

// Convert value to string.
if ctx != nil {
// Tracing values.
Expand Down
42 changes: 23 additions & 19 deletions os/glog/glog_logger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ func (in *HandlerInput) String(withColor ...bool) string {

func (in *HandlerInput) getDefaultBuffer(withColor bool) *bytes.Buffer {
buffer := bytes.NewBuffer(nil)
if in.TimeFormat != "" {
buffer.WriteString(in.TimeFormat)
}
if in.LevelFormat != "" {
var levelStr = "[" + in.LevelFormat + "]"
if withColor {
in.addStringToBuffer(buffer, in.Logger.getColoredStr(
in.Logger.getColorByLevel(in.Level), levelStr,
))
} else {
in.addStringToBuffer(buffer, levelStr)
if in.Logger.config.HeaderPrint {
if in.TimeFormat != "" {
buffer.WriteString(in.TimeFormat)
}
if in.LevelFormat != "" {
var levelStr = "[" + in.LevelFormat + "]"
if withColor {
in.addStringToBuffer(buffer, in.Logger.getColoredStr(
in.Logger.getColorByLevel(in.Level), levelStr,
))
} else {
in.addStringToBuffer(buffer, levelStr)
}
}
}
if in.TraceId != "" {
Expand All @@ -100,14 +102,16 @@ func (in *HandlerInput) getDefaultBuffer(withColor bool) *bytes.Buffer {
if in.CtxStr != "" {
in.addStringToBuffer(buffer, "{"+in.CtxStr+"}")
}
if in.Prefix != "" {
in.addStringToBuffer(buffer, in.Prefix)
}
if in.CallerFunc != "" {
in.addStringToBuffer(buffer, in.CallerFunc)
}
if in.CallerPath != "" {
in.addStringToBuffer(buffer, in.CallerPath)
if in.Logger.config.HeaderPrint {
if in.Prefix != "" {
in.addStringToBuffer(buffer, in.Prefix)
}
if in.CallerFunc != "" {
in.addStringToBuffer(buffer, in.CallerFunc)
}
if in.CallerPath != "" {
in.addStringToBuffer(buffer, in.CallerPath)
}
}
if in.Content != "" {
if in.Stack != "" {
Expand Down
2 changes: 1 addition & 1 deletion util/gutil/gutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Try(ctx context.Context, try func(ctx context.Context)) (err error) {
}

// TryCatch implements try...catch... logistics using internal panic...recover.
// It automatically calls function `catch` if any exception occurs ans passes the exception as an error.
// It automatically calls function `catch` if any exception occurs and passes the exception as an error.
func TryCatch(ctx context.Context, try func(ctx context.Context), catch ...func(ctx context.Context, exception error)) {
defer func() {
if exception := recover(); exception != nil && len(catch) > 0 {
Expand Down