Search code examples
gologrus

Logrus: Set log level based on package name through configuration


In the Java world it's relatively common to create one logger per package/class- something like:

final static Logger logger = Logger.getLogger("foo.bar.baz");

This means that in configuration we can set different log levels for different packages:

log4j.logger.foo.bar=info
log4j.logger.foo.bar.baz=debug

This is really nice because in a large, complex, app it means you can silence noisy 3rd party libraries, or alternatively enable debug logging only in the package(s) that you think maybe causing the issue.

My question is whether there is anything similar for logrus and, if there isn't, whether any other go logging libraries offer this sort of functionality.


Solution

  • yes

    use zap

    https://github.com/uber-go/zap

    
    const (
          named = "foo.bar.baz"
    )
    
    func main() {
        logger.Named(named).Error("foo")
    }
    
    

    you can manage it by package