diff options
Diffstat (limited to 'modules/log/log.go')
-rw-r--r-- | modules/log/log.go | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/modules/log/log.go b/modules/log/log.go index 0ca0f3adc5..71e88491f1 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -8,13 +8,35 @@ import ( "os" "runtime" "strings" + "sync" ) +type loggerMap struct { + sync.Map +} + +func (m *loggerMap) Load(k string) (*Logger, bool) { + v, ok := m.Map.Load(k) + if !ok { + return nil, false + } + l, ok := v.(*Logger) + return l, ok +} + +func (m *loggerMap) Store(k string, v *Logger) { + m.Map.Store(k, v) +} + +func (m *loggerMap) Delete(k string) { + m.Map.Delete(k) +} + var ( // DEFAULT is the name of the default logger DEFAULT = "default" // NamedLoggers map of named loggers - NamedLoggers = make(map[string]*Logger) + NamedLoggers loggerMap prefix string ) @@ -25,16 +47,16 @@ func NewLogger(bufLen int64, name, provider, config string) *Logger { CriticalWithSkip(1, "Unable to create default logger: %v", err) panic(err) } - return NamedLoggers[DEFAULT] + l, _ := NamedLoggers.Load(DEFAULT) + return l } // NewNamedLogger creates a new named logger for a given configuration func NewNamedLogger(name string, bufLen int64, subname, provider, config string) error { - logger, ok := NamedLoggers[name] + logger, ok := NamedLoggers.Load(name) if !ok { logger = newLogger(name, bufLen) - - NamedLoggers[name] = logger + NamedLoggers.Store(name, logger) } return logger.SetLogger(subname, provider, config) @@ -42,16 +64,16 @@ func NewNamedLogger(name string, bufLen int64, subname, provider, config string) // DelNamedLogger closes and deletes the named logger func DelNamedLogger(name string) { - l, ok := NamedLoggers[name] + l, ok := NamedLoggers.Load(name) if ok { - delete(NamedLoggers, name) + NamedLoggers.Delete(name) l.Close() } } // DelLogger removes the named sublogger from the default logger func DelLogger(name string) error { - logger := NamedLoggers[DEFAULT] + logger, _ := NamedLoggers.Load(DEFAULT) found, err := logger.DelLogger(name) if !found { Trace("Log %s not found, no need to delete", name) @@ -61,21 +83,24 @@ func DelLogger(name string) error { // GetLogger returns either a named logger or the default logger func GetLogger(name string) *Logger { - logger, ok := NamedLoggers[name] + logger, ok := NamedLoggers.Load(name) if ok { return logger } - return NamedLoggers[DEFAULT] + logger, _ = NamedLoggers.Load(DEFAULT) + return logger } // GetLevel returns the minimum logger level func GetLevel() Level { - return NamedLoggers[DEFAULT].GetLevel() + l, _ := NamedLoggers.Load(DEFAULT) + return l.GetLevel() } // GetStacktraceLevel returns the minimum logger level func GetStacktraceLevel() Level { - return NamedLoggers[DEFAULT].GetStacktraceLevel() + l, _ := NamedLoggers.Load(DEFAULT) + return l.GetStacktraceLevel() } // Trace records trace log @@ -169,18 +194,18 @@ func IsFatal() bool { // Close closes all the loggers func Close() { - l, ok := NamedLoggers[DEFAULT] + l, ok := NamedLoggers.Load(DEFAULT) if !ok { return } - delete(NamedLoggers, DEFAULT) + NamedLoggers.Delete(DEFAULT) l.Close() } // Log a message with defined skip and at logging level // A skip of 0 refers to the caller of this command func Log(skip int, level Level, format string, v ...interface{}) { - l, ok := NamedLoggers[DEFAULT] + l, ok := NamedLoggers.Load(DEFAULT) if ok { l.Log(skip+1, level, format, v...) } @@ -195,7 +220,8 @@ type LoggerAsWriter struct { // NewLoggerAsWriter creates a Writer representation of the logger with setable log level func NewLoggerAsWriter(level string, ourLoggers ...*Logger) *LoggerAsWriter { if len(ourLoggers) == 0 { - ourLoggers = []*Logger{NamedLoggers[DEFAULT]} + l, _ := NamedLoggers.Load(DEFAULT) + ourLoggers = []*Logger{l} } l := &LoggerAsWriter{ ourLoggers: ourLoggers, |