From f580b7a4883465b3bcfac8caa69eacf4ef4ceb3b Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 29 Jul 2022 19:06:22 +0800 Subject: [PATCH] improve header printing in json format for package glog; add golang v1.18 support for ci workflow (#2037) --- .github/workflows/gf.yml | 2 +- net/ghttp/ghttp_request.go | 2 +- os/glog/glog_logger.go | 83 +++++++++++++++++----------------- os/glog/glog_logger_handler.go | 42 +++++++++-------- util/gutil/gutil.go | 2 +- 5 files changed, 67 insertions(+), 64 deletions(-) diff --git a/.github/workflows/gf.yml b/.github/workflows/gf.yml index 0cd2a70bc..bce8c6d6a 100644 --- a/.github/workflows/gf.yml +++ b/.github/workflows/gf.yml @@ -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: diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 7c61f8fd0..286024d96 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -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 diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index d4869a9b0..5ac72a035 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -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" - } - 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) + timeFormat += "15:04:05" + } + 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) + // 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 l.config.Flags&F_FILE_SHORT > 0 { - input.CallerPath = fmt.Sprintf(`%s:%d:`, gfile.Basename(path), line) - } + // 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) } } - // Prefix. - if len(l.config.Prefix) > 0 { - input.Prefix = l.config.Prefix + 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) + } } } + // Prefix. + if len(l.config.Prefix) > 0 { + input.Prefix = l.config.Prefix + } + // Convert value to string. if ctx != nil { // Tracing values. diff --git a/os/glog/glog_logger_handler.go b/os/glog/glog_logger_handler.go index 2c610e999..f500dbc92 100644 --- a/os/glog/glog_logger_handler.go +++ b/os/glog/glog_logger_handler.go @@ -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 != "" { @@ -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 != "" { diff --git a/util/gutil/gutil.go b/util/gutil/gutil.go index c8fbcfc0a..b6b76efbe 100644 --- a/util/gutil/gutil.go +++ b/util/gutil/gutil.go @@ -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 {