My notes while experimenting logrus as available here : https://github.com/sirupsen/logrus
Sample Program to start with :
package main import ( "fmt" log "github.com/sirupsen/logrus" ) /* // ParseLevel takes a string level and returns the Logrus log level constant. func ParseLevel(lvl string) (Level, error) { switch strings.ToLower(lvl) { case "panic": return PanicLevel, nil case "fatal": return FatalLevel, nil case "error": return ErrorLevel, nil case "warn", "warning": return WarnLevel, nil case "info": return InfoLevel, nil case "debug": return DebugLevel, nil case "trace": return TraceLevel, nil } var l Level return l, fmt.Errorf("not a valid logrus Level: %q", lvl) } */ func main() { log.SetFormatter(&log.JSONFormatter{}) log.SetLevel(log.DebugLevel) log.Println("Its Println") log.Print("Its Print") log.Trace("Something very low level.") log.Debug("Useful debugging information.") log.Info("Something noteworthy happened!") log.Warn("You should probably take a look at this.") log.Error("Something failed but I'm not quitting.") // Calls os.Exit(1) after logging //log.Fatal("Bye.") // Calls panic() after logging //log.Panic("I'm bailing.") fmt.Println("\n\n\n") log.WithFields(log.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") log.WithFields(log.Fields{ "omg": true, "number": 122, }).Warn("The group's number increased tremendously!") // A common pattern is to re-use fields between logging statements by re-using // the logrus.Entry returned from WithFields() contextLogger := log.WithFields(log.Fields{ "common": "this is a common field", "other": "I also should be logged always", }) contextLogger.Info("I'll be logged with common and other field") contextLogger.Info("Me too") fmt.Println("\n\n\n") log.SetFormatter(&log.TextFormatter{}) log.Println("Its Println") log.Print("Its Print") log.SetLevel(log.DebugLevel) log.Println("Println") log.Print("Print") log.Trace("Something very low level.") log.Debug("Useful debugging information.") log.Info("Something noteworthy happened!") log.Warn("You should probably take a look at this.") log.Error("Something failed but I'm not quitting.") // Calls os.Exit(1) after logging log.Fatal("Bye.") // Calls panic() after logging log.Panic("I'm bailing.") }
Output :
{“level”:”info”,”msg”:”Its Println”,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”info”,”msg”:”Its Print”,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”debug”,”msg”:”Useful debugging information.”,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”info”,”msg”:”Something noteworthy happened!”,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”warning”,”msg”:”You should probably take a look at this.”,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”error”,”msg”:”Something failed but I’m not quitting.”,”time”:”2020-09-24T19:25:59+10:00″}
{“animal”:”walrus”,”level”:”info”,”msg”:”A group of walrus emerges from the ocean”,”size”:10,”time”:”2020-09-24T19:25:59+10:00″}
{“level”:”warning”,”msg”:”The group’s number increased tremendously!”,”number”:122,”omg”:true,”time”:”2020-09-24T19:25:59+10:00″}
{“common”:”this is a common field”,”level”:”info”,”msg”:”I’ll be logged with common and other field”,”other”:”I also should be logged always”,”time”:”2020-09-24T19:25:59+10:00″}
{“common”:”this is a common field”,”level”:”info”,”msg”:”Me too”,”other”:”I also should be logged always”,”time”:”2020-09-24T19:25:59+10:00″}
INFO[0000] Its Println
INFO[0000] Its Print
INFO[0000] Println
INFO[0000] Print
DEBU[0000] Useful debugging information.
INFO[0000] Something noteworthy happened!
WARN[0000] You should probably take a look at this.
ERRO[0000] Something failed but I’m not quitting.
FATA[0000] Bye.
Process finished
esumits-MacBook-Pro:godev esumit$ go version
go version go1.12.7 darwin/amd64
esumits-MacBook-Pro:godev esumit$
package main import ( "fmt" nested "github.com/antonfisher/nested-logrus-formatter" "github.com/sirupsen/logrus" "github.com/zput/zxcTool/ztLog/zt_formatter" "path" "runtime" ) func main() { var exampleFormatter = &zt_formatter.ZtFormatter{ CallerPrettyfier: func(f *runtime.Frame) (string, string) { filename := path.Base(f.File) return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line) }, Formatter: nested.Formatter{ //HideKeys: true, FieldsOrder: []string{"component", "category"}, }, } printDemo(exampleFormatter, "hello world") } func printDemo(f logrus.Formatter, title string) { l := logrus.New() l.SetLevel(logrus.DebugLevel) l.SetReportCaller(true) if f != nil { l.SetFormatter(f) } l.Infof("this is %v demo", title) lWebServer := l.WithField("component", "web-server") lWebServer.Info("starting...") lWebServerReq := lWebServer.WithFields(logrus.Fields{ "req": "GET /api/stats", "reqId": "#1", }) lWebServerReq.Info("params: startYear=2048") lWebServerReq.Error("response: 400 Bad Request") lDbConnector := l.WithField("category", "db-connector") lDbConnector.Info("connecting to db on 10.10.10.13...") lDbConnector.Warn("connection took 10s") l.Info("demo end.") }
Output
GOROOT=/usr/local/go #gosetup
GOPATH=/Users/esumit/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/zx/p7stj86d2hd4qt2y92_prr700000gn/T/___go_build_newlog_go /Users/esumit/go/src/github.com/GoLangExamples/newlog.go #gosetup
/private/var/folders/zx/p7stj86d2hd4qt2y92_prr700000gn/T/___go_build_newlog_go #gosetup
[newlog.go:36::main.printDemo()] Sep 24 19:38:21.161 [I] this is hello world demo
[newlog.go:39::main.printDemo()] Sep 24 19:38:21.161 [I] [component:web-server] starting…
[newlog.go:46::main.printDemo()] Sep 24 19:38:21.161 [I] [component:web-server] [req:GET /api/stats] [reqId:#1] params: startYear=2048
[newlog.go:47::main.printDemo()] Sep 24 19:38:21.162 [E] [component:web-server] [req:GET /api/stats] [reqId:#1] response: 400 Bad Request
[newlog.go:50::main.printDemo()] Sep 24 19:38:21.162 [I] [category:db-connector] connecting to db on 10.10.10.13…
[newlog.go:51::main.printDemo()] Sep 24 19:38:21.162 [W] [category:db-connector] connection took 10s
[newlog.go:53::main.printDemo()] Sep 24 19:38:21.162 [I] demo end.
Reference :
http://callistaenterprise.se/blogg/teknik/2017/08/02/go-blog-series-part10/