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
+5 -4
View File
@@ -83,19 +83,18 @@ func normalizeLogFilePath(logFilePath string) string {
// initConsoleLogging initializes console logging, then storing to mainLog.
func initConsoleLogging() {
consoleWriterLevel = zapcore.InfoLevel
consoleWriterLevel = ctrld.NoticeLevel
switch {
case silent:
// For silent mode, use a no-op logger
l := zap.NewNop()
mainLog.Store(&ctrld.Logger{Logger: l})
case verbose == 1:
// Info level is default
// Info level
consoleWriterLevel = zapcore.InfoLevel
case verbose > 1:
// Debug level
consoleWriterLevel = zapcore.DebugLevel
default:
// Notice level maps to Info in zap
}
consoleWriter = newHumanReadableZapCore(os.Stdout, consoleWriterLevel)
l := zap.New(consoleWriter)
@@ -172,6 +171,8 @@ func initLoggingWithBackup(doBackup bool) []zapcore.Core {
level = zapcore.DebugLevel
case "info":
level = zapcore.InfoLevel
case "notice":
level = ctrld.NoticeLevel
case "warn":
level = zapcore.WarnLevel
case "error":