logging - Golang logrus - how to do a centralized configuration? -


i using logrus in go app. believe question applicable other logging package (which doesn't offer external file based configuration) well.

logrus provides functions setup various configuration, e.g. setoutput, setlevel etc.

like other application need logging multiple source files/packages, seems need setup these options in each file logrus.

is there way setup these options once somewhere in central place shared on application. way if have make logging level change can in 1 place , applies components of app.

you don't need set these options in each file logrus.

you can import logrus log:

import log "github.com/sirupsen/logrus" 

then functions log.setoutput() functions , modify global logger , apply file includes import.

you can create package global log variable:

var log = logrus.new() 

then functions log.setoutput() methods , modify package global. awkward imo if have multiple packages in program, because each of them has different logger different settings (but maybe that's use cases). don't approach because confuses goimports (which want insert log imports list).

or can create own wrapper (which do). have own log package own logger var:

var logger = logrus.new() 

then make top-level functions wrap logrus:

func info(args ...interface{}) {     logger.info(args...) }  func debug(args ...interface{}) {     logger.debug(args...) } 

this tedious, allows me add functions specific program:

func withconn(conn net.conn) *logrus.entry {     var addr string = "unknown"     if conn != nil {         addr = conn.remoteaddr().string()     }     return logger.withfield("addr", addr) }  func withrequest(req *http.request) *logrus.entry {     return logger.withfields(requestfields(req)) } 

so can things like:

log.withconn(c).info("connected") 

(i plan in future wrap logrus.entry own type can chain these better; can't call log.withconn(c).withrequest(r).error(...) because can't add withrequest() logrus.entry.)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -