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

This commit is contained in:
John Guo
2022-07-29 19:06:22 +08:00
committed by GitHub
parent 9df0a9da0a
commit f580b7a488
5 changed files with 67 additions and 64 deletions

View File

@ -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:

View File

@ -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

View File

@ -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.

View File

@ -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 != "" {

View File

@ -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 {