Open yanmxa opened 10 hours ago
zap is highly flexible and allows you to configure multiple log levels, providing detailed control over how logs are recorded. However, when logging key-value pairs, you must use zapcore.Filed
to structure the key/value entries
logger.Info("Info log", zapcore.Field{Key: "key", String: "val"})
zapr builds on zap's flexibility, reusing the same configuration. The key difference is that zapr exposes a higher-level, user-friendly interface for logging key-value pairs. Instead of manually using zapcore.Field, you can directly log with a cleaner syntax:
log.V(0).Info("info", "key", "val")
The cons
V(0)
typically corresponds to INFO
, and V(1)
to DEBUG
, levels beyond V(1) may not behave as expected, as zap doesn't directly map them.DEBUG
, INFO
, and ERROR
levels are sufficient and works well for typical use cases.zc.Level.SetLevel(-2)
...
log.V(2).Info("-2heloo2", "ni", "hao")
...
2024-10-23T21:00:22.363+0800 LEVEL(-2) log-zapr/main.go:83 -2heloo2 {"ni": "hao"}
Suggest using the zapr, which can be invoked by the interface logr easily. The only concern is it only supports DEBUG
, INFO
, and ERROR
Here's a comprehensive comparison of the specified logging libraries based on various features, including log format, log level configuration, dynamic change log level, log code location, and time formatting: