Skip to content

Commit

Permalink
chore: add comments (#4618)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jan 31, 2025
1 parent 84db9bc commit 4e71e95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
10 changes: 5 additions & 5 deletions core/logx/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,23 +560,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, combineGlobalFields(addCaller(fields...))...)
getWriter().Debug(val, mergeGlobalFields(addCaller(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, combineGlobalFields(addCaller(fields...))...)
getWriter().Error(val, mergeGlobalFields(addCaller(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, combineGlobalFields(addCaller(fields...))...)
getWriter().Info(val, mergeGlobalFields(addCaller(fields...))...)
}

// writeSevere writes v into severe log.
Expand All @@ -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.
Expand All @@ -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())...)
}
5 changes: 3 additions & 2 deletions core/logx/richlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)...)
}
}

Expand Down
40 changes: 26 additions & 14 deletions core/logx/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 4e71e95

Please sign in to comment.