feat: add custom NOTICE log level between INFO and WARN

- Add NoticeLevel constant using zapcore.WarnLevel value (1)
- Implement custom level encoders (noticeLevelEncoder, noticeColorLevelEncoder)
- Update Notice() method to use custom level
- Add "notice" case to log level parsing in main.go
- Update encoder configurations to handle NOTICE level properly
- Add comprehensive test (TestNoticeLevel) to verify behavior

The NOTICE level provides visual distinction from INFO and ERROR levels,
with cyan color in development and proper level filtering. When log level
is set to NOTICE, it shows NOTICE and above (WARN, ERROR) while filtering
out DEBUG and INFO messages.

Note: NOTICE and WARN share the same numeric value (1) due to zap's
integer-based level system, so both display as "NOTICE" in logs for
visual consistency.

Usage:
- logger.Notice().Msg("message")
- log_level = "notice" in config
- Supports structured logging with fields
This commit is contained in:
Cuong Manh Le
2025-07-22 20:10:55 +07:00
committed by Cuong Manh Le
parent f933664e7d
commit f73a17f25d
4 changed files with 100 additions and 7 deletions
+9 -1
View File
@@ -10,6 +10,14 @@ import (
"go.uber.org/zap/zapcore"
)
// Custom log level for NOTICE (between INFO and WARN)
// DEBUG = -1, INFO = 0, WARN = 1, ERROR = 2, FATAL = 3
// Since there's no integer between INFO (0) and WARN (1), we'll use the same value as WARN
// but handle NOTICE specially in the encoder to display it differently.
// Note: NOTICE and WARN share the same numeric value (1), so they will both display as "NOTICE"
// when using the custom encoder. This is the intended behavior for visual distinction.
const NoticeLevel = zapcore.Level(zapcore.WarnLevel) // Same value as WARN, but handled specially
// LoggerCtxKey is the context.Context key for a logger.
type LoggerCtxKey struct{}
@@ -169,7 +177,7 @@ func (l *Logger) Fatal() *LogEvent {
func (l *Logger) Notice() *LogEvent {
return &LogEvent{
logger: l.Logger,
level: zapcore.InfoLevel, // zap doesn't have Notice level, use Info
level: NoticeLevel, // Custom NOTICE level between INFO and WARN
fields: []zap.Field{},
}
}