From afde6aea11f7f572803b22b193308050d67b33a1 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Mon, 10 Mar 2014 19:52:39 -0400 Subject: [PATCH] levels: add to logger type --- README.md | 16 ++++++++-------- entry.go | 37 +++++++++++++++---------------------- hooks.go | 6 +++--- logger.go | 4 +++- logrus.go | 16 +++++++--------- 5 files changed, 36 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 5dc4493..ed74500 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ seen as a hint you want to add a field, however, you can still use the #### Hooks You can add hooks for logging levels. For example to send errors to an exception -tracking service on `LevelError`, `LevelFatal` and `LevelPanic`. +tracking service on `Error`, `Fatal` and `Panic`. ```go log = logrus.New() @@ -51,12 +51,12 @@ func (hook *AirbrakeHook) Fire(entry *Entry) (error) { return nil } -// `Levels()` returns a slice of `LevelTypes` the hook is fired for. -func (hook *AirbrakeHook) Levels() []logrus.LevelType { - return []logrus.LevelType{ - logrus.LevelError, - logrus.LevelFatal, - logrus.LevelPanic +// `Levels()` returns a slice of `Levels` the hook is fired for. +func (hook *AirbrakeHook) Levels() []logrus.Level { + return []logrus.Level{ + logrus.Error, + logrus.Fatal, + logrus.Panic } } ``` @@ -78,7 +78,7 @@ You can set the logging level: ```go // Will log anything that is info or above, default. -logrus.Level = LevelInfo +log.Level = logrus.Info ``` #### Entries diff --git a/entry.go b/entry.go index d79e345..d98e43e 100644 --- a/entry.go +++ b/entry.go @@ -79,52 +79,45 @@ func (entry *Entry) log(level string, msg string) string { } func (entry *Entry) Debug(args ...interface{}) { - if Level >= LevelDebug { + if entry.logger.Level >= Debug { entry.log("debug", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelDebug, entry) + entry.logger.Hooks.Fire(Debug, entry) } } func (entry *Entry) Info(args ...interface{}) { - if Level >= LevelInfo { + if entry.logger.Level >= Info { entry.log("info", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelInfo, entry) - } -} - -func (entry *Entry) Print(args ...interface{}) { - if Level >= LevelInfo { - entry.log("info", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelInfo, entry) + entry.logger.Hooks.Fire(Info, entry) } } func (entry *Entry) Warn(args ...interface{}) { - if Level >= LevelWarn { + if entry.logger.Level >= Warn { entry.log("warning", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelWarn, entry) + entry.logger.Hooks.Fire(Warn, entry) } } func (entry *Entry) Error(args ...interface{}) { - if Level >= LevelError { + if entry.logger.Level >= Error { entry.log("error", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelError, entry) + entry.logger.Hooks.Fire(Error, entry) } } func (entry *Entry) Fatal(args ...interface{}) { - if Level >= LevelFatal { + if entry.logger.Level >= Fatal { entry.log("fatal", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelFatal, entry) + entry.logger.Hooks.Fire(Fatal, entry) } os.Exit(1) } func (entry *Entry) Panic(args ...interface{}) { - if Level >= LevelPanic { + if entry.logger.Level >= Panic { msg := entry.log("panic", fmt.Sprint(args...)) - entry.logger.Hooks.Fire(LevelPanic, entry) + entry.logger.Hooks.Fire(Panic, entry) panic(msg) } panic(fmt.Sprint(args...)) @@ -141,7 +134,7 @@ func (entry *Entry) Infof(format string, args ...interface{}) { } func (entry *Entry) Printf(format string, args ...interface{}) { - entry.Print(fmt.Sprintf(format, args...)) + entry.Info(fmt.Sprintf(format, args...)) } func (entry *Entry) Warnf(format string, args ...interface{}) { @@ -153,7 +146,7 @@ func (entry *Entry) Warningf(format string, args ...interface{}) { } func (entry *Entry) Errorf(format string, args ...interface{}) { - entry.Print(fmt.Sprintf(format, args...)) + entry.Error(fmt.Sprintf(format, args...)) } func (entry *Entry) Fatalf(format string, args ...interface{}) { @@ -175,7 +168,7 @@ func (entry *Entry) Infoln(args ...interface{}) { } func (entry *Entry) Println(args ...interface{}) { - entry.Print(fmt.Sprint(args...)) + entry.Info(fmt.Sprint(args...)) } func (entry *Entry) Warnln(args ...interface{}) { diff --git a/hooks.go b/hooks.go index fe5f032..afb2bf9 100644 --- a/hooks.go +++ b/hooks.go @@ -1,11 +1,11 @@ package logrus type Hook interface { - Levels() []LevelType + Levels() []Level Fire(*Entry) error } -type levelHooks map[LevelType][]Hook +type levelHooks map[Level][]Hook func (hooks levelHooks) Add(hook Hook) { for _, level := range hook.Levels() { @@ -17,7 +17,7 @@ func (hooks levelHooks) Add(hook Hook) { } } -func (hooks levelHooks) Fire(level LevelType, entry *Entry) error { +func (hooks levelHooks) Fire(level Level, entry *Entry) error { for _, hook := range hooks[level] { if err := hook.Fire(entry); err != nil { return err diff --git a/logger.go b/logger.go index 3379469..22e6ebc 100644 --- a/logger.go +++ b/logger.go @@ -10,6 +10,7 @@ type Logger struct { Out io.Writer Hooks levelHooks Formatter Formatter + Level Level mu sync.Mutex } @@ -18,6 +19,7 @@ func New() *Logger { Out: os.Stdout, // Default to stdout, change it if you want. Formatter: new(TextFormatter), Hooks: make(levelHooks), + Level: Info, } } @@ -74,7 +76,7 @@ func (logger *Logger) Info(args ...interface{}) { } func (logger *Logger) Print(args ...interface{}) { - NewEntry(logger).Print(args...) + NewEntry(logger).Info(args...) } func (logger *Logger) Warn(args ...interface{}) { diff --git a/logrus.go b/logrus.go index bff336f..0257410 100644 --- a/logrus.go +++ b/logrus.go @@ -2,19 +2,17 @@ package logrus type Fields map[string]interface{} -type LevelType uint8 +type Level uint8 const ( - LevelPanic LevelType = iota - LevelFatal - LevelError - LevelWarn - LevelInfo - LevelDebug + Panic Level = iota + Fatal + Error + Warn + Info + Debug ) -var Level LevelType = LevelInfo - // StandardLogger is what your logrus-enabled library should take, that way // it'll accept a stdlib logger and a logrus logger. There's no standard // interface, this is the closest we get, unfortunately.