Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.
This repository is based on the Logrus with some customization, it is compability with the official Logrus, for detail usage about Logrus, please go to this link Logrus.
Below are the list of all my customization:
A project often contains a lot of modules, when we turn on the debug message, actually all debug message from all modules will be printed and flooded the console. I want to only output the debug message for some particular modules and still keep other modules operate on the default logging level. The "Module Level Logging" is designed for this.
fooLogger := logrus.NewModule("foo")
fooLogger.Debug("some message")
barLogger := logrus.NewModule("bar")
barLogger.Debug("some other message")
logrus.SetModuleLevel("foo", logrus.LevelDebug)
logrus.SetModuleLevel("bar", logrus.LevelInfo)
//You can set the logging level for different modules in following convenient way:
// For example: Set module "foo" in debug level, and "bar" in error level:
SetModuleLevelString("foo:debug, bar:error") //"foo:debug; bar:error" is also OK
//"foo:debug | bar:error" is also OK
// Set module "foo" in debug level, and "bar" in error level, and all others in info level:
SetModuleLevelString("foo:debug, bar:error, *:info")
// Set all modules to info level:
SetModuleLevelString("*:info")
// or even more simple:
SetModuleLevelString("info")
logrus.Trace("foo")
logrus.WithField("test", 123).Tracef("name=%s", "yyscamper")
logrus.WithStack().Debug("something happens")
//Set to automatically collect stacktrace if has error
logrus.SetStackOnError(true)
logrus.WithError(err).Error("file is not found")
The error wrapper yyscamper/errors can record the stacktrace when the error happens (not when logging happens), and also the error wrapper can attach some additional fileds information, These stacktrace and fields information will be automatically extracted by logrus
import "github.com/yyscamper/errors"
import "github.com/yyscamper/logrus"
err := errors.New("some error").WithFiled("path", "/home/me/foo.txt")
logrus.WithError(err).Error("failed to do something")
GO's default formatter for map and structure is ugly, I find a pretty formatter davecgh/go-spew. To incorporate into logrus, I forked it and also did a little customization, see my repo yyscamper/go-spew for detail.
Base on go-spew
, I created a new formatter for loggrus PrettyTextFormatter
, it outputs the data in a pretty style.
logrus.SetFormatter(&log.PrettyTextFormatter{})
A sugar wrapper for logrus.WithFields
, as I think this is more simple than logrus.WithFields(logrus.Fields{...})
logrus.With("key1", val1, "key2", val2, "key3", val3)
//Compared with following complex one:
logrus.WithFields(logrus.Fields{
"key1": val1,
"key2": val2,
"key3", val3,
})