From e5621dca87cc075c12683155e3f226dafd807559 Mon Sep 17 00:00:00 2001 From: Antoine Grondin Date: Tue, 15 Jul 2014 18:30:01 -0400 Subject: [PATCH] Export package func to a standard logger. --- README.md | 6 +-- entry.go | 48 ++++++++++----------- exported.go | 87 ++++++++++++++++++++++++++++++++++++++ hook_test.go | 26 ++++++------ hooks/airbrake/airbrake.go | 6 +-- logger.go | 2 +- logrus.go | 24 +++++------ 7 files changed, 143 insertions(+), 56 deletions(-) create mode 100644 exported.go diff --git a/README.md b/README.md index 41e23ef..0501a4c 100644 --- a/README.md +++ b/README.md @@ -137,9 +137,9 @@ func (hook *AirbrakeHook) Fire(entry *logrus.Entry) error { // `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, + logrus.ErrorLevel, + logrus.FatalLevel, + logrus.PanicLevel, } } ``` diff --git a/entry.go b/entry.go index 08cc15f..b706506 100644 --- a/entry.go +++ b/entry.go @@ -78,8 +78,8 @@ func (entry *Entry) log(level string, levelInt Level, msg string) string { } func (entry *Entry) Debug(args ...interface{}) { - if entry.Logger.Level >= Debug { - entry.log("debug", Debug, fmt.Sprint(args...)) + if entry.Logger.Level >= DebugLevel { + entry.log("debug", DebugLevel, fmt.Sprint(args...)) } } @@ -88,33 +88,33 @@ func (entry *Entry) Print(args ...interface{}) { } func (entry *Entry) Info(args ...interface{}) { - if entry.Logger.Level >= Info { - entry.log("info", Info, fmt.Sprint(args...)) + if entry.Logger.Level >= InfoLevel { + entry.log("info", InfoLevel, fmt.Sprint(args...)) } } func (entry *Entry) Warn(args ...interface{}) { - if entry.Logger.Level >= Warn { - entry.log("warning", Warn, fmt.Sprint(args...)) + if entry.Logger.Level >= WarnLevel { + entry.log("warning", WarnLevel, fmt.Sprint(args...)) } } func (entry *Entry) Error(args ...interface{}) { - if entry.Logger.Level >= Error { - entry.log("error", Error, fmt.Sprint(args...)) + if entry.Logger.Level >= ErrorLevel { + entry.log("error", ErrorLevel, fmt.Sprint(args...)) } } func (entry *Entry) Fatal(args ...interface{}) { - if entry.Logger.Level >= Fatal { - entry.log("fatal", Fatal, fmt.Sprint(args...)) + if entry.Logger.Level >= FatalLevel { + entry.log("fatal", FatalLevel, fmt.Sprint(args...)) } os.Exit(1) } func (entry *Entry) Panic(args ...interface{}) { - if entry.Logger.Level >= Panic { - msg := entry.log("panic", Panic, fmt.Sprint(args...)) + if entry.Logger.Level >= PanicLevel { + msg := entry.log("panic", PanicLevel, fmt.Sprint(args...)) panic(msg) } panic(fmt.Sprint(args...)) @@ -123,13 +123,13 @@ func (entry *Entry) Panic(args ...interface{}) { // Entry Printf family functions func (entry *Entry) Debugf(format string, args ...interface{}) { - if entry.Logger.Level >= Debug { + if entry.Logger.Level >= DebugLevel { entry.Debug(fmt.Sprintf(format, args...)) } } func (entry *Entry) Infof(format string, args ...interface{}) { - if entry.Logger.Level >= Info { + if entry.Logger.Level >= InfoLevel { entry.Info(fmt.Sprintf(format, args...)) } } @@ -139,7 +139,7 @@ func (entry *Entry) Printf(format string, args ...interface{}) { } func (entry *Entry) Warnf(format string, args ...interface{}) { - if entry.Logger.Level >= Warn { + if entry.Logger.Level >= WarnLevel { entry.Warn(fmt.Sprintf(format, args...)) } } @@ -149,19 +149,19 @@ func (entry *Entry) Warningf(format string, args ...interface{}) { } func (entry *Entry) Errorf(format string, args ...interface{}) { - if entry.Logger.Level >= Error { + if entry.Logger.Level >= ErrorLevel { entry.Error(fmt.Sprintf(format, args...)) } } func (entry *Entry) Fatalf(format string, args ...interface{}) { - if entry.Logger.Level >= Fatal { + if entry.Logger.Level >= FatalLevel { entry.Fatal(fmt.Sprintf(format, args...)) } } func (entry *Entry) Panicf(format string, args ...interface{}) { - if entry.Logger.Level >= Panic { + if entry.Logger.Level >= PanicLevel { entry.Panic(fmt.Sprintf(format, args...)) } } @@ -169,13 +169,13 @@ func (entry *Entry) Panicf(format string, args ...interface{}) { // Entry Println family functions func (entry *Entry) Debugln(args ...interface{}) { - if entry.Logger.Level >= Debug { + if entry.Logger.Level >= DebugLevel { entry.Debug(entry.sprintlnn(args...)) } } func (entry *Entry) Infoln(args ...interface{}) { - if entry.Logger.Level >= Info { + if entry.Logger.Level >= InfoLevel { entry.Info(entry.sprintlnn(args...)) } } @@ -185,7 +185,7 @@ func (entry *Entry) Println(args ...interface{}) { } func (entry *Entry) Warnln(args ...interface{}) { - if entry.Logger.Level >= Warn { + if entry.Logger.Level >= WarnLevel { entry.Warn(entry.sprintlnn(args...)) } } @@ -195,19 +195,19 @@ func (entry *Entry) Warningln(args ...interface{}) { } func (entry *Entry) Errorln(args ...interface{}) { - if entry.Logger.Level >= Error { + if entry.Logger.Level >= ErrorLevel { entry.Error(entry.sprintlnn(args...)) } } func (entry *Entry) Fatalln(args ...interface{}) { - if entry.Logger.Level >= Fatal { + if entry.Logger.Level >= FatalLevel { entry.Fatal(entry.sprintlnn(args...)) } } func (entry *Entry) Panicln(args ...interface{}) { - if entry.Logger.Level >= Panic { + if entry.Logger.Level >= PanicLevel { entry.Panic(entry.sprintlnn(args...)) } } diff --git a/exported.go b/exported.go new file mode 100644 index 0000000..b34cb64 --- /dev/null +++ b/exported.go @@ -0,0 +1,87 @@ +package logrus + +import ( + "io" +) + +var ( + // std is the name of the standard logger in stdlib `log` + std = New() +) + +// SetOutput sets the standard logger output. +func SetOutput(out io.Writer) { + std.mu.Lock() + defer std.mu.Unlock() + std.Out = out +} + +// SetFormatter sets the standard logger formatter. +func SetFormatter(formatter Formatter) { + std.mu.Lock() + defer std.mu.Unlock() + std.Formatter = formatter +} + +// SetLevel sets the standard logger level. +func SetLevel(level Level) { + std.mu.Lock() + defer std.mu.Unlock() + std.Level = level +} + +// AddHook adds a hook to the standard logger hooks. +func AddHook(hook Hook) { + std.mu.Lock() + defer std.mu.Unlock() + std.Hooks.Add(hook) +} + +// WithField creates an entry from the standard logger and adds a field to +// it. If you want multiple fields, use `WithFields`. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithField(key string, value interface{}) *Entry { + return std.WithField(key, value) +} + +// WithFields creates an entry from the standard logger and adds multiple +// fields to it. This is simply a helper for `WithField`, invoking it +// once for each field. +// +// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal +// or Panic on the Entry it returns. +func WithFields(fields Fields) *Entry { + return std.WithFields(fields) +} + +// Debug logs a message at level Debug on the standard logger. +func Debug(args ...interface{}) { + std.Debug(args) +} + +// Info logs a message at level Info on the standard logger. +func Info(args ...interface{}) { + std.Info(args) +} + +// Warn logs a message at level Warn on the standard logger. +func Warn(args ...interface{}) { + std.Warn(args) +} + +// Error logs a message at level Error on the standard logger. +func Error(args ...interface{}) { + std.Error(args) +} + +// Panic logs a message at level Panic on the standard logger. +func Panic(args ...interface{}) { + std.Panic(args) +} + +// Fatal logs a message at level Fatal on the standard logger. +func Fatal(args ...interface{}) { + std.Fatal(args) +} diff --git a/hook_test.go b/hook_test.go index 24a02c3..13f34cb 100644 --- a/hook_test.go +++ b/hook_test.go @@ -17,12 +17,12 @@ func (hook *TestHook) Fire(entry *Entry) error { func (hook *TestHook) Levels() []Level { return []Level{ - Debug, - Info, - Warn, - Error, - Fatal, - Panic, + DebugLevel, + InfoLevel, + WarnLevel, + ErrorLevel, + FatalLevel, + PanicLevel, } } @@ -49,12 +49,12 @@ func (hook *ModifyHook) Fire(entry *Entry) error { func (hook *ModifyHook) Levels() []Level { return []Level{ - Debug, - Info, - Warn, - Error, - Fatal, - Panic, + DebugLevel, + InfoLevel, + WarnLevel, + ErrorLevel, + FatalLevel, + PanicLevel, } } @@ -95,7 +95,7 @@ func (hook *ErrorHook) Fire(entry *Entry) error { func (hook *ErrorHook) Levels() []Level { return []Level{ - Error, + ErrorLevel, } } diff --git a/hooks/airbrake/airbrake.go b/hooks/airbrake/airbrake.go index d92dc88..880d21e 100644 --- a/hooks/airbrake/airbrake.go +++ b/hooks/airbrake/airbrake.go @@ -47,8 +47,8 @@ func (hook *AirbrakeHook) Fire(entry *logrus.Entry) error { func (hook *AirbrakeHook) Levels() []logrus.Level { return []logrus.Level{ - logrus.Error, - logrus.Fatal, - logrus.Panic, + logrus.ErrorLevel, + logrus.FatalLevel, + logrus.PanicLevel, } } diff --git a/logger.go b/logger.go index a545afd..7374fe3 100644 --- a/logger.go +++ b/logger.go @@ -47,7 +47,7 @@ func New() *Logger { Out: os.Stdout, Formatter: new(TextFormatter), Hooks: make(levelHooks), - Level: Info, + Level: InfoLevel, } } diff --git a/logrus.go b/logrus.go index 2376db3..f41e1a4 100644 --- a/logrus.go +++ b/logrus.go @@ -13,22 +13,22 @@ type Level uint8 // These are the different logging levels. You can set the logging level to log // on your instance of logger, obtained with `logrus.New()`. const ( - // Panic level, highest level of severity. Logs and then calls panic with the + // PanicLevel level, highest level of severity. Logs and then calls panic with the // message passed to Debug, Info, ... - Panic Level = iota - // Fatal level. Logs and then calls `os.Exit(1)`. It will exit even if the + PanicLevel Level = iota + // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the // logging level is set to Panic. - Fatal - // Error level. Logs. Used for errors that should definitely be noted. + FatalLevel + // ErrorLevel level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. - Error - // Warn level. Non-critical entries that deserve eyes. - Warn - // Info level. General operational entries about what's going on inside the + ErrorLevel + // WarnLevel level. Non-critical entries that deserve eyes. + WarnLevel + // InfoLevel level. General operational entries about what's going on inside the // application. - Info - // Debug level. Usually only enabled when debugging. Very verbose logging. - Debug + InfoLevel + // DebugLevel level. Usually only enabled when debugging. Very verbose logging. + DebugLevel ) // Won't compile if StdLogger can't be realized by a log.Logger