Documentation ¶
Overview ¶
Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
The simplest way to use Logrus is simply the package-level exported logger:
package main import ( log "github.com/xitonix/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", "number": 1, "size": 10, }).Write("A walrus appears") }
Output:
time="2015-09-07T08:48:33Z" level=info msg="A walrus appears" animal=walrus number=1 size=10
For a full guide visit https://github.com/xitonix/logrus
Example (Basic) ¶
package main import ( "github.com/xitonix/logrus" "os" ) func main() { var log = logrus.New(logrus.DebugLevel) log.SetFormatter(new(logrus.JSONFormatter)) log.SetFormatter(&logrus.TextFormatter{ DisableSorting: false, DisableTimestamp: true, }) log.SetOutput(os.Stdout) // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) // if err == nil { // log.SetOutput(file) // } else { // log.Info("Failed to log to file, using default stderr") // } defer func() { err := recover() if err != nil { entry := *err.(**logrus.Entry) log.AsError().WithFields(logrus.Fields{ "omg": true, "err_animal": entry.Data["animal"], "err_size": entry.Data["size"], "err_level": entry.Level, "err_message": entry.Message, "number": 100, }).Write("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code } }() log.AsDebug().WithFields(logrus.Fields{ "animal": "walrus", "number": 8, }).Write("Started observing beach") log.AsInfo().WithFields(logrus.Fields{ "animal": "walrus", "size": 10, }).Write("A group of walrus emerges from the ocean") log.AsWarning().WithFields(logrus.Fields{ "omg": true, "number": 122, }).Write("The group's number increased tremendously!") log.AsDebug().WithFields(logrus.Fields{ "temperature": -4, }).Write("Temperature changes") log.AsPanic().WithFields(logrus.Fields{ "animal": "orca", "size": 9009, }).Write("It's over 9000!") }
Output: level=debug msg="Started observing beach" animal=walrus number=8 level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 level=warning msg="The group's number increased tremendously!" number=122 omg=true level=debug msg="Temperature changes" temperature=-4 level=panic msg="It's over 9000!" animal=orca size=9009 level=error msg="The ice breaks!" err_animal=orca err_level=panic err_message="It's over 9000!" err_size=9009 number=100 omg=true
Index ¶
- Variables
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Debugln(args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(args ...interface{})
- func Exit(code int)
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(args ...interface{})
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func Panic(args ...interface{})
- func Panicf(format string, args ...interface{})
- func Panicln(args ...interface{})
- func RegisterExitHandler(handler func())
- func SetFormatter(formatter Formatter)
- func SetLevel(level Level)
- func SetOutput(out io.Writer)
- func UseJsonFormatter()
- func UseTextFormatter()
- func Warning(args ...interface{})
- func Warningf(format string, args ...interface{})
- func Warningln(args ...interface{})
- type Entry
- func AsDebug() *Entry
- func AsError() *Entry
- func AsFatal() *Entry
- func AsInfo() *Entry
- func AsLevel(level Level) *Entry
- func AsPanic() *Entry
- func AsWarning() *Entry
- func NewEntry(logger *Logger) *Entry
- func NewEntryWithField(logger *Logger, key string, value interface{}) *Entry
- func NewEntryWithFields(logger *Logger, fields Fields) *Entry
- func WithError(err error) *Entry
- func WithField(key string, value interface{}) *Entry
- func WithFields(fields Fields) *Entry
- func (entry *Entry) AsDebug() *Entry
- func (entry *Entry) AsError() *Entry
- func (entry *Entry) AsFatal() *Entry
- func (entry *Entry) AsInfo() *Entry
- func (entry *Entry) AsLevel(level Level) *Entry
- func (entry *Entry) AsPanic() *Entry
- func (entry *Entry) AsWarning() *Entry
- func (entry *Entry) String() (string, error)
- func (entry *Entry) WithError(err error) *Entry
- func (entry *Entry) WithField(key string, value interface{}) *Entry
- func (entry *Entry) WithFields(fields Fields) *Entry
- func (entry *Entry) Write(args ...interface{})
- func (entry *Entry) Writef(format string, args ...interface{})
- func (entry *Entry) Writeln(args ...interface{})
- func (entry *Entry) Writer() *io.PipeWriter
- func (entry *Entry) WriterLevel(level Level) *io.PipeWriter
- type FieldLogger
- type FieldMap
- type Fields
- type Formatter
- type JSONFormatter
- type Level
- type Logger
- func (logger *Logger) AsDebug() *Entry
- func (logger *Logger) AsError() *Entry
- func (logger *Logger) AsFatal() *Entry
- func (logger *Logger) AsInfo() *Entry
- func (logger *Logger) AsLevel(level Level) *Entry
- func (logger *Logger) AsPanic() *Entry
- func (logger *Logger) AsWarning() *Entry
- func (logger *Logger) Debug(args ...interface{})
- func (logger *Logger) Debugf(format string, args ...interface{})
- func (logger *Logger) Debugln(args ...interface{})
- func (logger *Logger) Error(args ...interface{})
- func (logger *Logger) Errorf(format string, args ...interface{})
- func (logger *Logger) Errorln(args ...interface{})
- func (logger *Logger) Fatal(args ...interface{})
- func (logger *Logger) Fatalf(format string, args ...interface{})
- func (logger *Logger) Fatalln(args ...interface{})
- func (logger *Logger) Info(args ...interface{})
- func (logger *Logger) Infof(format string, args ...interface{})
- func (logger *Logger) Infoln(args ...interface{})
- func (logger *Logger) Level() Level
- func (logger *Logger) Panic(args ...interface{})
- func (logger *Logger) Panicf(format string, args ...interface{})
- func (logger *Logger) Panicln(args ...interface{})
- func (logger *Logger) Print(args ...interface{})
- func (logger *Logger) Printf(format string, args ...interface{})
- func (logger *Logger) Println(args ...interface{})
- func (logger *Logger) SetFormatter(formatter Formatter)
- func (logger *Logger) SetLevel(level Level)
- func (logger *Logger) SetNoLock()
- func (logger *Logger) SetOutput(out io.Writer)
- func (logger *Logger) UseJsonFormatter()
- func (logger *Logger) UseTextFormatter()
- func (logger *Logger) Warning(args ...interface{})
- func (logger *Logger) Warningf(format string, args ...interface{})
- func (logger *Logger) Warningln(args ...interface{})
- func (logger *Logger) WithError(err error) *Entry
- func (logger *Logger) WithField(key string, value interface{}) *Entry
- func (logger *Logger) WithFields(fields Fields) *Entry
- func (logger *Logger) Writer() *io.PipeWriter
- func (logger *Logger) WriterLevel(level Level) *io.PipeWriter
- type MutexWrap
- type StdLogger
- type Termios
- type TextFormatter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AllLevels = []Level{ PanicLevel, FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel, }
A constant exposing all logging levels
Functions ¶
func Debug ¶
func Debug(args ...interface{})
Debug logs a Message at level Debug on the standard Logger.
func Debugf ¶ added in v0.4.1
func Debugf(format string, args ...interface{})
Debugf logs a Message at level Debug on the standard Logger.
func Debugln ¶ added in v0.4.1
func Debugln(args ...interface{})
Debugln logs a Message at level Debug on the standard Logger.
func Error ¶
func Error(args ...interface{})
Error logs a Message at level Error on the standard Logger.
func Errorf ¶ added in v0.4.1
func Errorf(format string, args ...interface{})
Errorf logs a Message at level Error on the standard Logger.
func Errorln ¶ added in v0.4.1
func Errorln(args ...interface{})
Errorln logs a Message at level Error on the standard Logger.
func Exit ¶ added in v0.11.0
func Exit(code int)
Exit runs all the Logrus atexit handlers and then terminates the program using os.Exit(code)
func Fatal ¶
func Fatal(args ...interface{})
Fatal logs a Message at level Fatal on the standard Logger.
func Fatalf ¶ added in v0.4.1
func Fatalf(format string, args ...interface{})
Fatalf logs a Message at level Fatal on the standard Logger.
func Fatalln ¶ added in v0.4.1
func Fatalln(args ...interface{})
Fatalln logs a Message at level Fatal on the standard Logger.
func Info ¶
func Info(args ...interface{})
Info logs a Message at level Info on the standard Logger.
func Infof ¶ added in v0.4.1
func Infof(format string, args ...interface{})
Infof logs a Message at level Info on the standard Logger.
func Infoln ¶ added in v0.4.1
func Infoln(args ...interface{})
Infoln logs a Message at level Info on the standard Logger.
func Panic ¶
func Panic(args ...interface{})
Panic logs a Message at level Panic on the standard Logger.
func Panicf ¶ added in v0.4.1
func Panicf(format string, args ...interface{})
Panicf logs a Message at level Panic on the standard Logger.
func Panicln ¶ added in v0.4.1
func Panicln(args ...interface{})
Panicln logs a Message at level Panic on the standard Logger.
func RegisterExitHandler ¶ added in v0.11.0
func RegisterExitHandler(handler func())
RegisterExitHandler adds a Logrus Exit handler, call logrus.Exit to invoke all handlers. The handlers will also be invoked when any Fatal log entry is made.
This method is useful when a caller wishes to use logrus to log a fatal message but also needs to gracefully shutdown. An example usecase could be closing database connections, or sending a alert that the application is closing.
func SetFormatter ¶ added in v0.4.0
func SetFormatter(formatter Formatter)
SetFormatter sets the standard Logger formatter.
func UseJsonFormatter ¶ added in v1.0.5
func UseJsonFormatter()
UseJsonFormatter sets the standard Logger's formatter to Json.
This method is a shortcut to SetFormatter(...) method.
func UseTextFormatter ¶ added in v1.0.5
func UseTextFormatter()
UseTextFormatter sets the standard Logger's formatter to text.
This method is a shortcut to SetFormatter(...) method.
func Warning ¶ added in v0.4.1
func Warning(args ...interface{})
Warning logs a Message at level Warn on the standard Logger.
Types ¶
type Entry ¶
type Entry struct { Logger *Logger // Data contains all the fields set by the user. Data Fields // Time at which the log entry was created Time time.Time // Level the log entry was logged at: Debug, Info, Warn, Error, Fatal or Panic Level Level // Message passed to Write method Message string }
Entry an entry is the final or intermediate Logrus logging entry. It contains all the fields passed with WithField{,s}. It's finally logged when Write method is called. These objects can be reused and passed around as much as you wish to avoid field duplication.
func AsDebug ¶ added in v1.0.4
func AsDebug() *Entry
AsDebug creates a new entry from the standard Logger and sets the level to `debug` Make sure you call this method before calling WithField, WithFields and WithError methods
func AsError ¶ added in v1.0.4
func AsError() *Entry
AsError creates a new entry from the standard Logger and sets the level to `error` Make sure you call this method before calling WithField, WithFields and WithError methods
func AsFatal ¶ added in v1.0.4
func AsFatal() *Entry
AsFatal creates a new entry from the standard Logger and sets the level to `fatal` Make sure you call this method before calling WithField, WithFields and WithError methods
func AsInfo ¶ added in v1.0.4
func AsInfo() *Entry
AsInfo creates a new entry from the standard Logger and sets the level to `info` Make sure you call this method before calling WithField, WithFields and WithError methods
func AsLevel ¶ added in v1.0.4
AsLevel creates a new entry from the standard Logger and sets the level to the specified value. Make sure you call this method before calling WithField, WithFields and WithError methods
func AsPanic ¶ added in v1.0.4
func AsPanic() *Entry
AsPanic creates a new entry from the standard Logger and sets the level to `panic` Make sure you call this method before calling WithField, WithFields and WithError methods
func AsWarning ¶ added in v1.0.4
func AsWarning() *Entry
AsWarning creates a new entry from the standard Logger and sets the level to `warning` Make sure you call this method before calling WithField, WithFields and WithError methods
func NewEntryWithField ¶ added in v1.0.4
NewEntryWithField creates a new log entry and adds a field to the entry If you want multiple fields, use `NewEntryWithFields`
func NewEntryWithFields ¶ added in v1.0.4
NewEntryWithFields creates a new log entry and adds a struct of fields to the entry
func WithError ¶ added in v0.8.7
WithError creates an entry from the standard Logger and adds an error to the entry.
func WithField ¶ added in v0.4.0
WithField creates an entry from the standard Logger and adds a field to the entry. 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 WithFields ¶ added in v0.4.0
WithFields creates an entry from the standard Logger and adds multiple fields to the entry.
Note that it doesn't log until you call Write method on the entry object it returns.
func (*Entry) AsDebug ¶ added in v1.0.4
AsDebug clones the entry into a new log entry and sets the level to `debug` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsError ¶ added in v1.0.4
AsError clones the entry into a new log entry and sets the level to `error` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsFatal ¶ added in v1.0.4
AsFatal clones the entry into a new log entry and sets the level to `fatal` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsInfo ¶ added in v1.0.4
AsInfo clones the entry into a new log entry and sets the level to `info` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsLevel ¶ added in v1.0.4
AsLevel clones the entry into a new log entry and sets the level to the specified value. Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsPanic ¶ added in v1.0.4
AsPanic clones the entry into a new log entry and sets the level to `panic` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) AsWarning ¶ added in v1.0.4
AsWarning clones the entry into a new log entry and sets the level to `warning` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Entry) String ¶
String returns the string representation from the reader and ultimately the formatter.
func (*Entry) WithField ¶
WithField adds a field to the log entry, note that it doesn't log until you call Write.
func (*Entry) WithFields ¶
WithFields adds a struct of fields to the log entry
func (*Entry) Writer ¶ added in v0.11.5
func (entry *Entry) Writer() *io.PipeWriter
func (*Entry) WriterLevel ¶ added in v0.11.5
func (entry *Entry) WriterLevel(level Level) *io.PipeWriter
type FieldLogger ¶ added in v0.10.0
type FieldLogger interface { WithField(key string, value interface{}) *Entry WithFields(fields Fields) *Entry WithError(err error) *Entry Debugf(format string, args ...interface{}) Infof(format string, args ...interface{}) Warningf(format string, args ...interface{}) Errorf(format string, args ...interface{}) Fatalf(format string, args ...interface{}) Panicf(format string, args ...interface{}) Debug(args ...interface{}) Info(args ...interface{}) Warning(args ...interface{}) Error(args ...interface{}) Fatal(args ...interface{}) Panic(args ...interface{}) Debugln(args ...interface{}) Infoln(args ...interface{}) Warningln(args ...interface{}) Errorln(args ...interface{}) Fatalln(args ...interface{}) Panicln(args ...interface{}) }
The FieldLogger interface generalizes the Entry and Logger types
type FieldMap ¶ added in v0.11.1
type FieldMap map[fieldKey]string
FieldMap allows customization of the key names for default fields.
type Formatter ¶
The formatter interface is used to implement a custom formatter. It takes an `Entry`. It exposes all the fields, including the default ones:
* `entry.Data["msg"]`. The Message passed from Info, Warn, Error .. * `entry.Data["time"]`. The timestamp. * `entry.Data["level"]. The level the entry was logged at.
Any additional fields added with `WithField` or `WithFields` are also in `entry.Data`. Format is expected to return an array of bytes which are then logged to `Logger.out`.
type JSONFormatter ¶
type JSONFormatter struct { // TimestampFormat sets the format used for marshaling timestamps. TimestampFormat string // DisableTimestamp allows disabling automatic timestamps in output DisableTimestamp bool // FieldMap allows users to customize the names of keys for default fields. // As an example: // formatter := &JSONFormatter{ // FieldMap: FieldMap{ // FieldKeyTime: "@timestamp", // FieldKeyLevel: "@level", // FieldKeyMsg: "@Message", // }, // } FieldMap FieldMap }
JSONFormatter formats logs into parsable json
type Level ¶
type Level uint32
level type
const ( // PanicLevel level, highest level of severity. Logs and then calls panic with the // Message passed to Debug, Info, ... PanicLevel Level = iota // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the // logging level is set to Panic. FatalLevel // ErrorLevel level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. ErrorLevel // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel // InfoLevel level. General operational entries about what's going on inside the // application. InfoLevel // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel )
These are the different logging levels. You can set the logging level to log on your instance of Logger, obtained with `logrus.New()`.
func ParseLevel ¶ added in v0.6.0
ParseLevel takes a string level and returns the Logrus log level constant.
type Logger ¶
type Logger struct { // Out The logs are `io.Copy`'d to this in a mutex. It's common to set this to a // file, or leave it default which is `os.Stderr`. You can also set this to // something more adventurous, such as logging to Kafka. Out io.Writer // contains filtered or unexported fields }
func New ¶
New creates a new instance of Logger. Configuration should be set by changing `formatter` (default TextFormatter), `Out` (default os.Stderr) and `hooks` directly on the default Logger instance. It's recommended to make this a global instance called `log`.
func StandardLogger ¶ added in v0.6.5
func StandardLogger() *Logger
StandardLogger returns a new Logger which writes to os.Stderr with the default TextFormatter
func (*Logger) AsDebug ¶ added in v1.0.4
AsDebug creates a new entry and sets the level to `debug` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsError ¶ added in v1.0.4
AsError creates a new entry and sets the level to `error` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsFatal ¶ added in v1.0.4
AsFatal creates a new entry and sets the level to `fatal` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsInfo ¶ added in v1.0.4
AsInfo creates a new entry and sets the level to `info` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsLevel ¶ added in v1.0.4
AsLevel creates a new entry and sets the level to the specified value. Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsPanic ¶ added in v1.0.4
AsPanic creates a new entry and sets the level to `panic` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) AsWarning ¶ added in v1.0.4
AsWarning creates a new entry and sets the level to `warning` Make sure you call this method before calling WithField, WithFields and WithError methods
func (*Logger) Debug ¶
func (logger *Logger) Debug(args ...interface{})
Debug logs a message at debug level
func (*Logger) Debugln ¶
func (logger *Logger) Debugln(args ...interface{})
Debugln logs a message followed by a new line at debug level
func (*Logger) Error ¶
func (logger *Logger) Error(args ...interface{})
Error logs a message at error level
func (*Logger) Errorln ¶
func (logger *Logger) Errorln(args ...interface{})
Errorln logs a message followed by a new line at error level
func (*Logger) Fatal ¶
func (logger *Logger) Fatal(args ...interface{})
Fatal logs a message at fatal level and terminates the app with exit code 1
func (*Logger) Fatalf ¶
Fatalf logs a formatted string at fatal level and terminated the execution of the application with exit code 1
func (*Logger) Fatalln ¶
func (logger *Logger) Fatalln(args ...interface{})
Fatalln logs a message followed by a new line at fatal level and terminates the execution of the application with exit code 1
func (*Logger) Info ¶
func (logger *Logger) Info(args ...interface{})
Info logs a message at info level
func (*Logger) Infoln ¶
func (logger *Logger) Infoln(args ...interface{})
Infoln logs a message followed by a new line at info level
func (*Logger) Panic ¶
func (logger *Logger) Panic(args ...interface{})
Panic logs a message at panic level and terminates the execution of the application with a panic
func (*Logger) Panicf ¶
Panicf logs a formatted string at panic level and terminates the execution of the application with a panic
func (*Logger) Panicln ¶
func (logger *Logger) Panicln(args ...interface{})
Panicln logs a message followed by a new line at panic level and terminates the execution of the application with a panic
func (*Logger) SetFormatter ¶ added in v1.0.4
SetFormatter sets the Logger's formatter.
You can also call UseJsonFormatter or UseTextFormatter shortcut functions Json and Text formatter
func (*Logger) SetNoLock ¶ added in v0.11.0
func (logger *Logger) SetNoLock()
SetNoLock when file is opened with appending mode, it's safe to write concurrently to a file (within 4k Message on Linux). In this case user can choose to disable the lock.
func (*Logger) UseJsonFormatter ¶ added in v1.0.5
func (logger *Logger) UseJsonFormatter()
UseJsonFormatter sets the log formatter to Json.
This method is a shortcut to SetFormatter(...) method.
func (*Logger) UseTextFormatter ¶ added in v1.0.5
func (logger *Logger) UseTextFormatter()
UseTextFormatter sets the log formatter to text.
This method is a shortcut to SetFormatter(...) method.
func (*Logger) Warning ¶
func (logger *Logger) Warning(args ...interface{})
Warning logs a message at warning level
func (*Logger) Warningln ¶
func (logger *Logger) Warningln(args ...interface{})
Warningln logs a message followed by a new line at warning level
func (*Logger) WithField ¶
WithField creates a new log entry object and adds a field to the entry. If you want multiple fields, use `WithFields`.
func (*Logger) WithFields ¶
WithFields creates a new log entry object and adds a struct of fields to the entry
func (*Logger) Writer ¶ added in v0.6.5
func (logger *Logger) Writer() *io.PipeWriter
func (*Logger) WriterLevel ¶ added in v0.11.0
func (logger *Logger) WriterLevel(level Level) *io.PipeWriter
type MutexWrap ¶ added in v0.11.0
type MutexWrap struct {
// contains filtered or unexported fields
}
type StdLogger ¶
type StdLogger interface { Print(...interface{}) Printf(string, ...interface{}) Println(...interface{}) Fatal(...interface{}) Fatalf(string, ...interface{}) Fatalln(...interface{}) Panic(...interface{}) Panicf(string, ...interface{}) Panicln(...interface{}) }
StdLogger 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.
type TextFormatter ¶
type TextFormatter struct { // Set to true to bypass checking for a TTY before outputting colors. ForceColors bool // Force disabling colors. DisableColors bool // Disable timestamp logging. useful when output is redirected to logging // system that already adds timestamps. DisableTimestamp bool // Enable logging the full timestamp when a TTY is attached instead of just // the time passed since beginning of execution. FullTimestamp bool // TimestampFormat to use for display when a full timestamp is printed TimestampFormat string // The fields are sorted by default for a consistent output. For applications // that log extremely frequently and don't use the JSON formatter this may not // be desired. DisableSorting bool // QuoteEmptyFields will wrap empty fields in quotes if true QuoteEmptyFields bool sync.Once // contains filtered or unexported fields }
TextFormatter formats logs into text