From 4e71e95e448155eac03c1015af4b0581b73b6c66 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 31 Jan 2025 22:42:46 +0800 Subject: [PATCH] chore: add comments (#4618) --- core/logx/logs.go | 10 +++++----- core/logx/richlogger.go | 5 +++-- core/logx/writer.go | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/core/logx/logs.go b/core/logx/logs.go index 7758cf6e4..447de86a1 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -560,7 +560,7 @@ 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, combineGlobalFields(addCaller(fields...))...) + getWriter().Debug(val, mergeGlobalFields(addCaller(fields...))...) } // writeError writes v into the error log. @@ -568,7 +568,7 @@ func writeDebug(val any, fields ...LogField) { // 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, combineGlobalFields(addCaller(fields...))...) + getWriter().Error(val, mergeGlobalFields(addCaller(fields...))...) } // writeInfo writes v into info log. @@ -576,7 +576,7 @@ func writeError(val any, fields ...LogField) { // 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, combineGlobalFields(addCaller(fields...))...) + getWriter().Info(val, mergeGlobalFields(addCaller(fields...))...) } // writeSevere writes v into severe log. @@ -592,7 +592,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, combineGlobalFields(addCaller(fields...))...) + getWriter().Slow(val, mergeGlobalFields(addCaller(fields...))...) } // writeStack writes v into stack log. @@ -608,5 +608,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, combineGlobalFields(addCaller())...) + getWriter().Stat(msg, mergeGlobalFields(addCaller())...) } diff --git a/core/logx/richlogger.go b/core/logx/richlogger.go index 7865a3ff9..dc80ead22 100644 --- a/core/logx/richlogger.go +++ b/core/logx/richlogger.go @@ -206,8 +206,9 @@ func (l *richLogger) WithFields(fields ...LogField) Logger { func (l *richLogger) buildFields(fields ...LogField) []LogField { fields = append(l.fields, fields...) + // caller field should always appear together with global fields fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip))) - fields = combineGlobalFields(fields) + fields = mergeGlobalFields(fields) if l.ctx == nil { return fields @@ -235,7 +236,7 @@ func (l *richLogger) buildFields(fields ...LogField) []LogField { func (l *richLogger) debug(v any, fields ...LogField) { if shallLog(DebugLevel) { - getWriter().Debug(v, (l.buildFields(fields...))...) + getWriter().Debug(v, l.buildFields(fields...)...) } } diff --git a/core/logx/writer.go b/core/logx/writer.go index d2bd5631f..ce57c811b 100644 --- a/core/logx/writer.go +++ b/core/logx/writer.go @@ -17,15 +17,27 @@ import ( ) type ( + // Writer is the interface for writing logs. + // It's designed to let users customize their own log writer, + // such as writing logs to a kafka, a database, or using third-party loggers. Writer interface { + // Alert sends an alert message, if your writer implemented alerting functionality. Alert(v any) + // Close closes the writer. Close() error + // Debug logs a message at debug level. Debug(v any, fields ...LogField) + // Error logs a message at error level. Error(v any, fields ...LogField) + // Info logs a message at info level. Info(v any, fields ...LogField) + // Severe logs a message at severe level. Severe(v any) + // Slow logs a message at slow level. Slow(v any, fields ...LogField) + // Stack logs a message at error level. Stack(v any) + // Stat logs a message at stat level. Stat(v any, fields ...LogField) } @@ -324,20 +336,6 @@ func buildPlainFields(fields logEntry) []string { return items } -func combineGlobalFields(fields []LogField) []LogField { - globals := globalFields.Load() - if globals == nil { - return fields - } - - gf := globals.([]LogField) - ret := make([]LogField, 0, len(gf)+len(fields)) - ret = append(ret, gf...) - ret = append(ret, fields...) - - return ret -} - func marshalJson(t interface{}) ([]byte, error) { var buf bytes.Buffer encoder := json.NewEncoder(&buf) @@ -352,6 +350,20 @@ func marshalJson(t interface{}) ([]byte, error) { return buf.Bytes(), err } +func mergeGlobalFields(fields []LogField) []LogField { + globals := globalFields.Load() + if globals == nil { + return fields + } + + gf := globals.([]LogField) + ret := make([]LogField, 0, len(gf)+len(fields)) + ret = append(ret, gf...) + ret = append(ret, fields...) + + return ret +} + func output(writer io.Writer, level string, val any, fields ...LogField) { // only truncate string content, don't know how to truncate the values of other types. if v, ok := val.(string); ok {