From 55c5e6e036685259f1fba039ccb9c831bacc5c91 Mon Sep 17 00:00:00 2001 From: Anuragkillswitch <70265851+Anuragkillswitch@users.noreply.github.com> Date: Sun, 14 May 2023 00:15:06 +0530 Subject: [PATCH] init: user defined log level --- zapcore/level.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/zapcore/level.go b/zapcore/level.go index e01a24131..f15143bf7 100644 --- a/zapcore/level.go +++ b/zapcore/level.go @@ -24,6 +24,7 @@ import ( "bytes" "errors" "fmt" + "sync" ) var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level") @@ -31,6 +32,36 @@ var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level") // A Level is a logging priority. Higher levels are more important. type Level int8 +type CustomLevel interface { + String() string + CapitalString() string + Enabled(lvl Level) bool +} + +// Type to hold custom levels +type CustomLevels struct { + mu sync.RWMutex + levels map[Level]CustomLevel +} + +// Global variable to hold all custom levels +var customLevels = &CustomLevels{ + levels: make(map[Level]CustomLevel), +} + +func (cl *CustomLevels) Add(level Level, custom CustomLevel) { + cl.mu.Lock() + defer cl.mu.Unlock() + cl.levels[level] = custom +} + +func (cl *CustomLevels) Get(level Level) (CustomLevel, bool) { + cl.mu.RLock() + defer cl.mu.RUnlock() + lvl, ok := cl.levels[level] + return lvl, ok +} + const ( // DebugLevel logs are typically voluminous, and are usually disabled in // production. @@ -111,6 +142,9 @@ func LevelOf(enab LevelEnabler) Level { // String returns a lower-case ASCII representation of the log level. func (l Level) String() string { + if customLevel, ok := customLevels.Get(l); ok { + return customLevel.String() + } switch l { case DebugLevel: return "debug" @@ -135,6 +169,10 @@ func (l Level) String() string { func (l Level) CapitalString() string { // Printing levels in all-caps is common enough that we should export this // functionality. + if customLevel, ok := customLevels.Get(l); ok { + return customLevel.CapitalString() + } + switch l { case DebugLevel: return "DEBUG" @@ -211,6 +249,9 @@ func (l *Level) Get() interface{} { // Enabled returns true if the given level is at or above this level. func (l Level) Enabled(lvl Level) bool { + if customLevel, ok := customLevels.Get(l); ok { + return customLevel.Enabled(lvl) + } return lvl >= l }