Logrus: Namespacing - name for each logger

Created on 7 May 2015  路  4Comments  路  Source: sirupsen/logrus

My suggestion is to add namespaces like in log4js. Example output for logger with name cheese

[2010-01-17 11:43:37.987] [ERRO] (cheese) Cheese is too ripe!

API could looks like this:

logger = logrus.New("myapp")
logger.Level = logrus.Level(config.Level)
webLogger := logger.New("web")
staticLogger := logger.New("static")
backgroundWorkerLogger := logger.New("worker")

and output could looks like:

[2010-01-17 11:43:37.987] [INFO] (myapp/web) HTTP 200 ...
[2010-01-17 11:43:37.987] [ERRO] (myapp/static) HTTP 404 ...
[2010-01-17 11:43:37.987] [ERRO] (myapp/worker) pq: connection lost ...

Most helpful comment

I would also find this extremely useful -- for example, if I'm building a layered application where I want to implement logging for both layers separately, it would be useful to "mute", say, the low-level protocol layer while building the application. Or in general to set different logging levels for the different namespaces.

All 4 comments

For now I use this workaround:

func Create(module  ...string) *logrus.Entry {
    if (len(module) > 0) {
        return logrus.WithField("module", module[0])
    }
    return logrus.WithField("module", nil)
}

var log = Create("myapp")
log.Info("Hello")

And custom formatter that considers module field to do correct output.
What is really wanted is that Entry and Logger implement some common defined interface with Info(), Debug(), LogWithFields() and so on.

This hasn't come up enough to get into core. Closing for now.

I would also find this extremely useful -- for example, if I'm building a layered application where I want to implement logging for both layers separately, it would be useful to "mute", say, the low-level protocol layer while building the application. Or in general to set different logging levels for the different namespaces.

Coming from Python where it's part of standard library - can please anyone explain how do you manage without it in Go? Logger name is the main hint about the origin of the message.

Was this page helpful?
0 / 5 - 0 ratings