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

fix: global fields not valid for third-party loggers #4616

Closed
wants to merge 1 commit into from
Closed
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
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
Loading