diff options
author | Jakob Borg <jakob@nym.se> | 2014-05-14 21:08:56 -0300 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-05-14 21:08:56 -0300 |
commit | cba554d0faae6c06a14307e3434eba229bd231f1 (patch) | |
tree | e5aad5310a29470961357701945857b36c987d96 /logger | |
parent | 8903825e020e51ed5cba5f105cdbad1988d3612d (diff) |
Refactor logging into separate package
Diffstat (limited to 'logger')
-rw-r--r-- | logger/logger.go | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000000..2053e146bb --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,142 @@ +package logger + +import ( + "fmt" + "log" + "os" + "sync" +) + +type LogLevel int + +const ( + LevelDebug LogLevel = iota + LevelInfo + LevelOK + LevelWarn + LevelFatal + NumLevels +) + +type MessageHandler func(l LogLevel, msg string) + +type Logger struct { + logger *log.Logger + handlers [NumLevels][]MessageHandler + mut sync.Mutex +} + +var DefaultLogger = New() + +func New() *Logger { + return &Logger{ + logger: log.New(os.Stderr, "", log.Ltime), + } +} + +func (l *Logger) AddHandler(level LogLevel, h MessageHandler) { + l.mut.Lock() + defer l.mut.Unlock() + l.handlers[level] = append(l.handlers[level], h) +} + +func (l *Logger) SetFlags(flag int) { + l.logger.SetFlags(flag) +} + +func (l *Logger) SetPrefix(prefix string) { + l.logger.SetPrefix(prefix) +} + +func (l *Logger) callHandlers(level LogLevel, s string) { + for _, h := range l.handlers[level] { + h(level, s) + } +} + +func (l *Logger) Debugln(vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintln(vals...) + l.logger.Output(2, "DEBUG: "+s) + l.callHandlers(LevelDebug, s) +} + +func (l *Logger) Debugf(format string, vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintf(format, vals...) + l.logger.Output(2, "DEBUG: "+s) + l.callHandlers(LevelDebug, s) +} +func (l *Logger) Infoln(vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintln(vals...) + l.logger.Output(2, "INFO: "+s) + l.callHandlers(LevelInfo, s) +} + +func (l *Logger) Infof(format string, vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintf(format, vals...) + l.logger.Output(2, "INFO: "+s) + l.callHandlers(LevelInfo, s) +} + +func (l *Logger) Okln(vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintln(vals...) + l.logger.Output(2, "OK: "+s) + l.callHandlers(LevelOK, s) +} + +func (l *Logger) Okf(format string, vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintf(format, vals...) + l.logger.Output(2, "OK: "+s) + l.callHandlers(LevelOK, s) +} + +func (l *Logger) Warnln(vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintln(vals...) + l.logger.Output(2, "WARNING: "+s) + l.callHandlers(LevelWarn, s) +} + +func (l *Logger) Warnf(format string, vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintf(format, vals...) + l.logger.Output(2, "WARNING: "+s) + l.callHandlers(LevelWarn, s) +} + +func (l *Logger) Fatalln(vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintln(vals...) + l.logger.Output(2, "FATAL: "+s) + l.callHandlers(LevelFatal, s) + os.Exit(3) +} + +func (l *Logger) Fatalf(format string, vals ...interface{}) { + l.mut.Lock() + defer l.mut.Unlock() + s := fmt.Sprintf(format, vals...) + l.logger.Output(2, "FATAL: "+s) + l.callHandlers(LevelFatal, s) + os.Exit(3) +} + +func (l *Logger) FatalErr(err error) { + if err != nil { + l.Fatalf(err.Error()) + } +} |