-
Notifications
You must be signed in to change notification settings - Fork 5
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
feature: add use_local_time_encoder and show_caller config #59
base: master
Are you sure you want to change the base?
Changes from 9 commits
ecfe574
6d265f2
583814d
2583ed9
07267aa
bb8c385
29bc270
0e06e7a
c1430a7
1305a3f
5c738a0
cf91fbf
9b65581
25d29ba
48d6db1
d8a4101
e322c8c
5b64a6a
b952530
571f8b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||
package logger | ||||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"fmt" | ||||||||||||||
"os" | ||||||||||||||
"strings" | ||||||||||||||
"time" | ||||||||||||||
|
@@ -95,15 +96,31 @@ | |||||||||||||
|
||||||||||||||
// File logger options | ||||||||||||||
FileLogger *FileLoggerConfig `mapstructure:"file_logger_options"` | ||||||||||||||
|
||||||||||||||
// UseLocalTime is used to set the encoder to use local time instead of UTC. | ||||||||||||||
UseLocalTime bool `mapstructure:"use_local_time"` | ||||||||||||||
|
||||||||||||||
// ShowCaller is used to set the encoder to show the caller. | ||||||||||||||
ShowCaller bool `mapstructure:"show_caller"` | ||||||||||||||
rustatian marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// BuildLogger converts config into Zap configuration. | ||||||||||||||
func (cfg *Config) BuildLogger() (*zap.Logger, error) { | ||||||||||||||
var zCfg zap.Config | ||||||||||||||
var callerKey = zapcore.OmitKey | ||||||||||||||
var encodeCaller zapcore.CallerEncoder = nil | ||||||||||||||
if cfg.ShowCaller { | ||||||||||||||
callerKey = "caller" | ||||||||||||||
encodeCaller = ColoredShortCallerEncoder | ||||||||||||||
} | ||||||||||||||
switch Mode(strings.ToLower(string(cfg.Mode))) { | ||||||||||||||
case off, none: | ||||||||||||||
return zap.NewNop(), nil | ||||||||||||||
case production: | ||||||||||||||
encodeTime := utcEpochTimeEncoder | ||||||||||||||
if cfg.UseLocalTime { | ||||||||||||||
encodeTime = localTimeEncoder | ||||||||||||||
} | ||||||||||||||
zCfg = zap.Config{ | ||||||||||||||
Level: zap.NewAtomicLevelAt(zap.InfoLevel), | ||||||||||||||
Development: false, | ||||||||||||||
|
@@ -112,20 +129,24 @@ | |||||||||||||
TimeKey: "ts", | ||||||||||||||
LevelKey: "level", | ||||||||||||||
NameKey: "logger", | ||||||||||||||
CallerKey: zapcore.OmitKey, | ||||||||||||||
CallerKey: callerKey, | ||||||||||||||
FunctionKey: zapcore.OmitKey, | ||||||||||||||
MessageKey: "msg", | ||||||||||||||
StacktraceKey: zapcore.OmitKey, | ||||||||||||||
LineEnding: cfg.LineEnding, | ||||||||||||||
EncodeLevel: zapcore.LowercaseLevelEncoder, | ||||||||||||||
EncodeTime: utcEpochTimeEncoder, | ||||||||||||||
EncodeTime: encodeTime, | ||||||||||||||
EncodeDuration: zapcore.SecondsDurationEncoder, | ||||||||||||||
EncodeCaller: zapcore.ShortCallerEncoder, | ||||||||||||||
EncodeCaller: encodeCaller, | ||||||||||||||
}, | ||||||||||||||
OutputPaths: []string{"stderr"}, | ||||||||||||||
ErrorOutputPaths: []string{"stderr"}, | ||||||||||||||
} | ||||||||||||||
case development: | ||||||||||||||
encodeTime := utcISO8601TimeEncoder | ||||||||||||||
if cfg.UseLocalTime { | ||||||||||||||
encodeTime = localTimeEncoder | ||||||||||||||
} | ||||||||||||||
zCfg = zap.Config{ | ||||||||||||||
Level: zap.NewAtomicLevelAt(zap.DebugLevel), | ||||||||||||||
Development: true, | ||||||||||||||
|
@@ -134,16 +155,16 @@ | |||||||||||||
TimeKey: "ts", | ||||||||||||||
LevelKey: "level", | ||||||||||||||
NameKey: "logger", | ||||||||||||||
CallerKey: zapcore.OmitKey, | ||||||||||||||
CallerKey: callerKey, | ||||||||||||||
FunctionKey: zapcore.OmitKey, | ||||||||||||||
MessageKey: "msg", | ||||||||||||||
StacktraceKey: zapcore.OmitKey, | ||||||||||||||
LineEnding: cfg.LineEnding, | ||||||||||||||
EncodeLevel: ColoredLevelEncoder, | ||||||||||||||
EncodeName: ColoredNameEncoder, | ||||||||||||||
EncodeTime: utcISO8601TimeEncoder, | ||||||||||||||
EncodeTime: encodeTime, | ||||||||||||||
EncodeDuration: zapcore.StringDurationEncoder, | ||||||||||||||
EncodeCaller: zapcore.ShortCallerEncoder, | ||||||||||||||
EncodeCaller: encodeCaller, | ||||||||||||||
}, | ||||||||||||||
OutputPaths: []string{"stderr"}, | ||||||||||||||
ErrorOutputPaths: []string{"stderr"}, | ||||||||||||||
|
@@ -160,23 +181,27 @@ | |||||||||||||
ErrorOutputPaths: []string{"stderr"}, | ||||||||||||||
} | ||||||||||||||
default: | ||||||||||||||
encodeTime := utcISO8601TimeEncoder | ||||||||||||||
if cfg.UseLocalTime { | ||||||||||||||
encodeTime = localTimeEncoder | ||||||||||||||
} | ||||||||||||||
zCfg = zap.Config{ | ||||||||||||||
Level: zap.NewAtomicLevelAt(zap.DebugLevel), | ||||||||||||||
Encoding: "console", | ||||||||||||||
EncoderConfig: zapcore.EncoderConfig{ | ||||||||||||||
TimeKey: "T", | ||||||||||||||
LevelKey: "L", | ||||||||||||||
NameKey: "N", | ||||||||||||||
CallerKey: zapcore.OmitKey, | ||||||||||||||
CallerKey: callerKey, | ||||||||||||||
FunctionKey: zapcore.OmitKey, | ||||||||||||||
MessageKey: "M", | ||||||||||||||
StacktraceKey: zapcore.OmitKey, | ||||||||||||||
LineEnding: cfg.LineEnding, | ||||||||||||||
EncodeLevel: ColoredLevelEncoder, | ||||||||||||||
EncodeName: ColoredNameEncoder, | ||||||||||||||
EncodeTime: utcISO8601TimeEncoder, | ||||||||||||||
EncodeTime: encodeTime, | ||||||||||||||
EncodeDuration: zapcore.StringDurationEncoder, | ||||||||||||||
EncodeCaller: zapcore.ShortCallerEncoder, | ||||||||||||||
EncodeCaller: encodeCaller, | ||||||||||||||
}, | ||||||||||||||
OutputPaths: []string{"stderr"}, | ||||||||||||||
ErrorOutputPaths: []string{"stderr"}, | ||||||||||||||
|
@@ -206,6 +231,11 @@ | |||||||||||||
// init it | ||||||||||||||
// otherwise - return standard config | ||||||||||||||
if cfg.FileLogger != nil { | ||||||||||||||
fileEncoderConfig := zCfg.EncoderConfig | ||||||||||||||
if cfg.ShowCaller { | ||||||||||||||
fileEncoderConfig.EncodeCaller = ShortCallerEncoderWithPadding | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// init absent options | ||||||||||||||
cfg.FileLogger.InitDefaults() | ||||||||||||||
|
||||||||||||||
|
@@ -220,10 +250,13 @@ | |||||||||||||
) | ||||||||||||||
|
||||||||||||||
core := zapcore.NewCore( | ||||||||||||||
zapcore.NewJSONEncoder(zCfg.EncoderConfig), | ||||||||||||||
zapcore.NewJSONEncoder(fileEncoderConfig), | ||||||||||||||
w, | ||||||||||||||
zCfg.Level, | ||||||||||||||
) | ||||||||||||||
if cfg.ShowCaller { | ||||||||||||||
return zap.New(core, zap.AddCaller()), nil | ||||||||||||||
} | ||||||||||||||
return zap.New(core), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
@@ -251,3 +284,15 @@ | |||||||||||||
func utcEpochTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||||||||||||||
enc.AppendInt64(t.UTC().UnixNano()) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||||||||||||||
enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000")) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, ISO8601 encode format should be used here to be in sync with the current encoder. |
||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider maintaining consistent time format across encoders. The Consider using the same ISO8601 format for consistency: func localTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
- enc.AppendString(t.Local().Format("2006-01-02 15:04:05.000"))
+ enc.AppendString(t.Local().Format("2006-01-02T15:04:05-0700"))
} 📝 Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
func ColoredShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { | ||||||||||||||
enc.AppendString(fmt.Sprintf("\x1b[35m%-40s\x1b[0m", caller.TrimmedPath())) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func ShortCallerEncoderWithPadding(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { | ||||||||||||||
enc.AppendString(fmt.Sprintf("%-40s", caller.TrimmedPath())) | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) | |
|
||
// ColoredNameEncoder colorizes service names. | ||
func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) { | ||
if len(s) < 12 { | ||
s += strings.Repeat(" ", 12-len(s)) | ||
if len(s) < 20 { | ||
s += strings.Repeat(" ", 20-len(s)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the service plugin displays the plugin name with the specific service name, the name is too long, and 12 characters are obviously not enough There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not enough for what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. such as : service
sync-data:
command: xxx the length of 'service.sync-data' is 17 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you don't understand what that code is supposed to do. If the len is, lets say: 17. In that case, the full service name would be written without omitting any character. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thus, I'm not really understand your motivation to increase that limit from 12 to 20. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry. I didn't look closely enough. I made a mistake |
||
|
||
enc.AppendString(color.HiGreenString(s)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,7 @@ http: | |
logs: | ||
mode: development | ||
level: debug | ||
use_local_time: true | ||
show_caller: true | ||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Documentation needed for new logging configuration options The new configuration options
🔗 Analysis chainLGTM! Well-structured configuration. The new logging options are correctly placed under the Let's verify the configuration schema documentation: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check if these new options are documented in the schema or docs
# Expected: Find references to these new config options in documentation
# Look for schema definitions or documentation
rg -g '*.{md,json,yaml}' -i "use.*local.*time|show.*caller"
Length of output: 174 Script: #!/bin/bash
# Let's check for any documentation or schema files that might define logger configuration
fd -e md -e yaml -e json . | grep -i "schema\|config\|logger" | xargs cat 2>/dev/null | grep -i "logger\|log"
Length of output: 2239 |
||
file_logger_options: | ||
log_output: "test.log" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I understand this. The caller is the line where your log is. How is that difficult to find where you send this log?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When developing custom plugins, although logs show what happened, pinpointing the exact source of a log entry in complex plugin code can be challenging without caller information. By enabling show_caller, we can immediately see which file and line generated the log, significantly improving debugging efficiency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can it be complex? It is just ctrl+f (or / for vim) -> enter log message -> here you are.