logrus.SetFormatter should be a member of instance logrus.New()

Created on 29 Jan 2018  路  8Comments  路  Source: sirupsen/logrus

logrus.SetFormatter should not be a package-level setting. A logrus instance can be passed all over an application, and should be carry with it the desired configuration of the application.

question

All 8 comments

Actually now I'm realizing that logrus.SetFormatter has no effect whatsoever.

found this in the code
// Creates a new logger. Configuration should be set by changingFormatter, //OutandHooksdirectly on the default logger instance. You can also just // instantiate your own: // // var log = &Logger{ // Out: os.Stderr, // Formatter: new(JSONFormatter), // Hooks: make(LevelHooks), // Level: logrus.DebugLevel, // } // // It's recommended to make this a global instance calledlog. func New() *Logger { return &Logger{ Out: os.Stderr, Formatter: new(TextFormatter), Hooks: make(LevelHooks), Level: InfoLevel, } }

Why don't you just write a declaration function where you set the format then return a instance?

func ConfigureLogging(someconfigdata) (*logrus.Logger) {
    logrus.SetFormatter(&log.JSONFormatter{})

    return log.StandardLogger() 
}

Actually now I'm realizing that logrus.SetFormatter has no effect whatsoever.

What? The package level SetFormatter call sets the formatter for the package global StandardLogger instance. This is the same instance that is used by package-level logging functions. Example, logrus.Errorf() will use the standard logger, and therefore use the formatter that you provided to SetFormatter. SetFormatter is ineffective if you're using a custom logger handle returned by logrus.New, but that's kinda by design, and certainly doesn't make SetFormatter useless. Setting the standard logger settings and simply calling the logrus package functions (better yet, by aliasing logrus as log in the import lines) is a very common and solid usage.

Why would a new logrus instance (from logrus.New) not support a custom formatter? It seems that assumes the only valid use case for logging is to use the package level logger, which seems unfortunately short-sighted.

Why would a new logrus instance (from logrus.New) not support a custom formatter? It seems that assumes the only valid use case for logging is to use the package level logger, which seems unfortunately short-sighted.

It...does? logrus.New() returns a *logrus.Logger. The logrus.Logger struct has an exported field Formatter. Just set that to whatever formatter you want. That logger handle will use that formatter, and the standard logger won't. The package level function _only_ alters the standard logger, not loggers created via logrus.New().

Or is your complaint that you have to set the Logger.Formatter field directly, instead of the Logger type having a SetFormatter method? Seems like a pretty superfluous complaint if so, given that the field (and many others) are intentionally exported to avoid the need for that type of method bloat.

@Yugloocamai indeed SetFormatter changes the logger formatter to the one given as parameter with appropriate locking.
You can do that on the global formatter directly with a call to logrus.SetFormatter or through the method of the same name applied to the instance you want to change the formatter.
But as you've seen in the documentation, the New function is here by convention as it is expected by most of go packages. You can also directly declare a Logger object and set the formatter to the value you want.

no activity since 2018, closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mildred picture mildred  路  3Comments

anotherGoogleFan picture anotherGoogleFan  路  3Comments

vagruchi picture vagruchi  路  5Comments

SergeiVasilenko picture SergeiVasilenko  路  3Comments

piotrkowalczuk picture piotrkowalczuk  路  4Comments