Skip to content

Commit

Permalink
fix: global fields not valid for third-party loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jan 31, 2025
1 parent b28f79a commit 6f87b54
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 7 additions & 6 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ func WithRotation(r string) LogOption {
}
}

func addCaller(fields ...LogField) []LogField {
func assembleFields(fields ...LogField) []LogField {
fields = combineGlobalFields(fields)
return append(fields, Field(callerKey, getCaller(callerDepth)))
}

Expand Down Expand Up @@ -560,23 +561,23 @@ func shallLogStat() bool {
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
// The caller should check shallLog before calling this function.
func writeDebug(val any, fields ...LogField) {
getWriter().Debug(val, addCaller(fields...)...)
getWriter().Debug(val, assembleFields(fields...)...)
}

// writeError writes v into the error log.
// Not checking shallLog here is for performance consideration.
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
// The caller should check shallLog before calling this function.
func writeError(val any, fields ...LogField) {
getWriter().Error(val, addCaller(fields...)...)
getWriter().Error(val, assembleFields(fields...)...)
}

// writeInfo writes v into info log.
// Not checking shallLog here is for performance consideration.
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
// The caller should check shallLog before calling this function.
func writeInfo(val any, fields ...LogField) {
getWriter().Info(val, addCaller(fields...)...)
getWriter().Info(val, assembleFields(fields...)...)
}

// writeSevere writes v into severe log.
Expand All @@ -592,7 +593,7 @@ func writeSevere(msg string) {
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
// The caller should check shallLog before calling this function.
func writeSlow(val any, fields ...LogField) {
getWriter().Slow(val, addCaller(fields...)...)
getWriter().Slow(val, assembleFields(fields...)...)
}

// writeStack writes v into stack log.
Expand All @@ -608,5 +609,5 @@ func writeStack(msg string) {
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
// The caller should check shallLog before calling this function.
func writeStat(msg string) {
getWriter().Stat(msg, addCaller()...)
getWriter().Stat(msg, assembleFields()...)
}
1 change: 0 additions & 1 deletion core/logx/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ func output(writer io.Writer, level string, val any, fields ...LogField) {
}
}

fields = combineGlobalFields(fields)
// +3 for timestamp, level and content
entry := make(logEntry, len(fields)+3)
for _, field := range fields {
Expand Down

0 comments on commit 6f87b54

Please sign in to comment.