Merge pull request #45 from aybabtme/export-package-level-log-funcs

Export package func to a standard logger.
This commit is contained in:
Simon Eskildsen 2014-07-26 20:48:56 -04:00
commit 6be56e6e50
7 changed files with 151 additions and 57 deletions

View File

@ -81,6 +81,13 @@ func main() {
} }
``` ```
#### Package logging
Alike the stdlib logger, logrus exposes functions that you can use to log
to a default global logger. This is convenient to avoid passing a
`logrus.Logger` thorough your app's packages; you can simply setup `logrus
from your main package and use the package function directly accross your app.
#### Fields #### Fields
Logrus encourages careful, structured logging though logging fields instead of Logrus encourages careful, structured logging though logging fields instead of
@ -94,7 +101,7 @@ log = logrus.New()
log.WithFields(logrus.Fields{ log.WithFields(logrus.Fields{
"event": event, "event": event,
"topic": topic, "topic": topic,
"key": key "key": key,
}).Fatal("Failed to send event") }).Fatal("Failed to send event")
``` ```
@ -140,9 +147,9 @@ func (hook *AirbrakeHook) Fire(entry *logrus.Entry) error {
// `Levels()` returns a slice of `Levels` the hook is fired for. // `Levels()` returns a slice of `Levels` the hook is fired for.
func (hook *AirbrakeHook) Levels() []logrus.Level { func (hook *AirbrakeHook) Levels() []logrus.Level {
return []logrus.Level{ return []logrus.Level{
logrus.Error, logrus.ErrorLevel,
logrus.Fatal, logrus.FatalLevel,
logrus.Panic, logrus.PanicLevel,
} }
} }
``` ```

View File

@ -78,8 +78,8 @@ func (entry *Entry) log(level string, levelInt Level, msg string) string {
} }
func (entry *Entry) Debug(args ...interface{}) { func (entry *Entry) Debug(args ...interface{}) {
if entry.Logger.Level >= Debug { if entry.Logger.Level >= DebugLevel {
entry.log("debug", Debug, fmt.Sprint(args...)) entry.log("debug", DebugLevel, fmt.Sprint(args...))
} }
} }
@ -88,33 +88,33 @@ func (entry *Entry) Print(args ...interface{}) {
} }
func (entry *Entry) Info(args ...interface{}) { func (entry *Entry) Info(args ...interface{}) {
if entry.Logger.Level >= Info { if entry.Logger.Level >= InfoLevel {
entry.log("info", Info, fmt.Sprint(args...)) entry.log("info", InfoLevel, fmt.Sprint(args...))
} }
} }
func (entry *Entry) Warn(args ...interface{}) { func (entry *Entry) Warn(args ...interface{}) {
if entry.Logger.Level >= Warn { if entry.Logger.Level >= WarnLevel {
entry.log("warning", Warn, fmt.Sprint(args...)) entry.log("warning", WarnLevel, fmt.Sprint(args...))
} }
} }
func (entry *Entry) Error(args ...interface{}) { func (entry *Entry) Error(args ...interface{}) {
if entry.Logger.Level >= Error { if entry.Logger.Level >= ErrorLevel {
entry.log("error", Error, fmt.Sprint(args...)) entry.log("error", ErrorLevel, fmt.Sprint(args...))
} }
} }
func (entry *Entry) Fatal(args ...interface{}) { func (entry *Entry) Fatal(args ...interface{}) {
if entry.Logger.Level >= Fatal { if entry.Logger.Level >= FatalLevel {
entry.log("fatal", Fatal, fmt.Sprint(args...)) entry.log("fatal", FatalLevel, fmt.Sprint(args...))
} }
os.Exit(1) os.Exit(1)
} }
func (entry *Entry) Panic(args ...interface{}) { func (entry *Entry) Panic(args ...interface{}) {
if entry.Logger.Level >= Panic { if entry.Logger.Level >= PanicLevel {
msg := entry.log("panic", Panic, fmt.Sprint(args...)) msg := entry.log("panic", PanicLevel, fmt.Sprint(args...))
panic(msg) panic(msg)
} }
panic(fmt.Sprint(args...)) panic(fmt.Sprint(args...))
@ -123,13 +123,13 @@ func (entry *Entry) Panic(args ...interface{}) {
// Entry Printf family functions // Entry Printf family functions
func (entry *Entry) Debugf(format string, args ...interface{}) { func (entry *Entry) Debugf(format string, args ...interface{}) {
if entry.Logger.Level >= Debug { if entry.Logger.Level >= DebugLevel {
entry.Debug(fmt.Sprintf(format, args...)) entry.Debug(fmt.Sprintf(format, args...))
} }
} }
func (entry *Entry) Infof(format string, args ...interface{}) { func (entry *Entry) Infof(format string, args ...interface{}) {
if entry.Logger.Level >= Info { if entry.Logger.Level >= InfoLevel {
entry.Info(fmt.Sprintf(format, args...)) 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{}) { func (entry *Entry) Warnf(format string, args ...interface{}) {
if entry.Logger.Level >= Warn { if entry.Logger.Level >= WarnLevel {
entry.Warn(fmt.Sprintf(format, args...)) 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{}) { func (entry *Entry) Errorf(format string, args ...interface{}) {
if entry.Logger.Level >= Error { if entry.Logger.Level >= ErrorLevel {
entry.Error(fmt.Sprintf(format, args...)) entry.Error(fmt.Sprintf(format, args...))
} }
} }
func (entry *Entry) Fatalf(format string, args ...interface{}) { func (entry *Entry) Fatalf(format string, args ...interface{}) {
if entry.Logger.Level >= Fatal { if entry.Logger.Level >= FatalLevel {
entry.Fatal(fmt.Sprintf(format, args...)) entry.Fatal(fmt.Sprintf(format, args...))
} }
} }
func (entry *Entry) Panicf(format string, args ...interface{}) { func (entry *Entry) Panicf(format string, args ...interface{}) {
if entry.Logger.Level >= Panic { if entry.Logger.Level >= PanicLevel {
entry.Panic(fmt.Sprintf(format, args...)) entry.Panic(fmt.Sprintf(format, args...))
} }
} }
@ -169,13 +169,13 @@ func (entry *Entry) Panicf(format string, args ...interface{}) {
// Entry Println family functions // Entry Println family functions
func (entry *Entry) Debugln(args ...interface{}) { func (entry *Entry) Debugln(args ...interface{}) {
if entry.Logger.Level >= Debug { if entry.Logger.Level >= DebugLevel {
entry.Debug(entry.sprintlnn(args...)) entry.Debug(entry.sprintlnn(args...))
} }
} }
func (entry *Entry) Infoln(args ...interface{}) { func (entry *Entry) Infoln(args ...interface{}) {
if entry.Logger.Level >= Info { if entry.Logger.Level >= InfoLevel {
entry.Info(entry.sprintlnn(args...)) entry.Info(entry.sprintlnn(args...))
} }
} }
@ -185,7 +185,7 @@ func (entry *Entry) Println(args ...interface{}) {
} }
func (entry *Entry) Warnln(args ...interface{}) { func (entry *Entry) Warnln(args ...interface{}) {
if entry.Logger.Level >= Warn { if entry.Logger.Level >= WarnLevel {
entry.Warn(entry.sprintlnn(args...)) entry.Warn(entry.sprintlnn(args...))
} }
} }
@ -195,19 +195,19 @@ func (entry *Entry) Warningln(args ...interface{}) {
} }
func (entry *Entry) Errorln(args ...interface{}) { func (entry *Entry) Errorln(args ...interface{}) {
if entry.Logger.Level >= Error { if entry.Logger.Level >= ErrorLevel {
entry.Error(entry.sprintlnn(args...)) entry.Error(entry.sprintlnn(args...))
} }
} }
func (entry *Entry) Fatalln(args ...interface{}) { func (entry *Entry) Fatalln(args ...interface{}) {
if entry.Logger.Level >= Fatal { if entry.Logger.Level >= FatalLevel {
entry.Fatal(entry.sprintlnn(args...)) entry.Fatal(entry.sprintlnn(args...))
} }
} }
func (entry *Entry) Panicln(args ...interface{}) { func (entry *Entry) Panicln(args ...interface{}) {
if entry.Logger.Level >= Panic { if entry.Logger.Level >= PanicLevel {
entry.Panic(entry.sprintlnn(args...)) entry.Panic(entry.sprintlnn(args...))
} }
} }

87
exported.go Normal file
View File

@ -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...)
}

View File

@ -17,12 +17,12 @@ func (hook *TestHook) Fire(entry *Entry) error {
func (hook *TestHook) Levels() []Level { func (hook *TestHook) Levels() []Level {
return []Level{ return []Level{
Debug, DebugLevel,
Info, InfoLevel,
Warn, WarnLevel,
Error, ErrorLevel,
Fatal, FatalLevel,
Panic, PanicLevel,
} }
} }
@ -49,12 +49,12 @@ func (hook *ModifyHook) Fire(entry *Entry) error {
func (hook *ModifyHook) Levels() []Level { func (hook *ModifyHook) Levels() []Level {
return []Level{ return []Level{
Debug, DebugLevel,
Info, InfoLevel,
Warn, WarnLevel,
Error, ErrorLevel,
Fatal, FatalLevel,
Panic, PanicLevel,
} }
} }
@ -95,7 +95,7 @@ func (hook *ErrorHook) Fire(entry *Entry) error {
func (hook *ErrorHook) Levels() []Level { func (hook *ErrorHook) Levels() []Level {
return []Level{ return []Level{
Error, ErrorLevel,
} }
} }

View File

@ -47,8 +47,8 @@ func (hook *AirbrakeHook) Fire(entry *logrus.Entry) error {
func (hook *AirbrakeHook) Levels() []logrus.Level { func (hook *AirbrakeHook) Levels() []logrus.Level {
return []logrus.Level{ return []logrus.Level{
logrus.Error, logrus.ErrorLevel,
logrus.Fatal, logrus.FatalLevel,
logrus.Panic, logrus.PanicLevel,
} }
} }

View File

@ -47,7 +47,7 @@ func New() *Logger {
Out: os.Stdout, Out: os.Stdout,
Formatter: new(TextFormatter), Formatter: new(TextFormatter),
Hooks: make(levelHooks), Hooks: make(levelHooks),
Level: Info, Level: InfoLevel,
} }
} }

View File

@ -13,22 +13,22 @@ type Level uint8
// These are the different logging levels. You can set the logging level to log // These are the different logging levels. You can set the logging level to log
// on your instance of logger, obtained with `logrus.New()`. // on your instance of logger, obtained with `logrus.New()`.
const ( 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, ... // message passed to Debug, Info, ...
Panic Level = iota PanicLevel Level = iota
// Fatal level. Logs and then calls `os.Exit(1)`. It will exit even if the // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
// logging level is set to Panic. // logging level is set to Panic.
Fatal FatalLevel
// Error level. Logs. Used for errors that should definitely be noted. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service. // Commonly used for hooks to send errors to an error tracking service.
Error ErrorLevel
// Warn level. Non-critical entries that deserve eyes. // WarnLevel level. Non-critical entries that deserve eyes.
Warn WarnLevel
// Info level. General operational entries about what's going on inside the // InfoLevel level. General operational entries about what's going on inside the
// application. // application.
Info InfoLevel
// Debug level. Usually only enabled when debugging. Very verbose logging. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
Debug DebugLevel
) )
// Won't compile if StdLogger can't be realized by a log.Logger // Won't compile if StdLogger can't be realized by a log.Logger