logger
Package logger is a wrapper of structured Zap Logger with extended features.
Features
- Config logger via env.
- Name key is optional, add custom prefix message.
- Add log duration functions (Infod, Debugd, Warnd, Autod).
- Add default logger instance in package
log
. - Share common logger across repositories.
- Easy to start with many simple ways to create a basic logger.
- Easy to switch between logger and sugared logger with
Wrap
and Unwrap
method.
Quick start
Install
go get -d github.com/carousell/gologger
Options
LOG_LEVEL="debug"
LOG_LEVEL_MAIN="debug"
LOG_COLOR="true"
LOG_ENCODING="console"
LOG_TIMESTAMP="rfc3339"
Example
package main
import (
"time"
logger "github.com/carousell/gologger"
)
var (
log = logger.MustNew()
)
func main() {
log.Info("start")
defer log.Infod()("execution time")
doSomething()
}
func doSomething() {
defer log.Debugd()("do A took")
defer log.Autod(1 * time.Second)("do B")
defer log.Autod(3 * time.Second)("do B")
userID := 123
log := log.Withf("[user:%d]", userID)
defer log.Warnd(1 * time.Second)("do C")
defer log.Warnd(3 * time.Second)("do C")
time.Sleep(2 * time.Second)
}
$ LOG_COLOR=true go run example/main.go
2021-10-26T18:00:30.792Z info example/main.go:22 start
2021-10-26T18:00:32.794Z warn example/main.go:54 [user:123] do C: 2.0010509s
2021-10-26T18:00:32.794Z debug example/main.go:54 do B: 2.0011264s
2021-10-26T18:00:32.794Z warn example/main.go:54 do B: 2.0011339s
2021-10-26T18:00:32.794Z debug example/main.go:54 do A took: 2.0011446s
2021-10-26T18:00:32.794Z info example/main.go:26 execution time: 2.0011492s
$ LOG_TIMESTAMP=ms LOG_ENCODING=json go run example/main.go
{"level":"info","ts":1635257396952,"caller":"example/main.go:22","msg":"start"}
{"level":"warn","ts":1635257398953,"caller":"example/main.go:54","msg":"[user:123] do C: 2.0010486s"}
{"level":"debug","ts":1635257398953,"caller":"example/main.go:54","msg":"do B: 2.0011176s"}
{"level":"warn","ts":1635257398953,"caller":"example/main.go:54","msg":"do B: 2.0011231s"}
{"level":"debug","ts":1635257398953,"caller":"example/main.go:54","msg":"do A took: 2.0011263s"}
{"level":"info","ts":1635257398953,"caller":"example/main.go:26","msg":"execution time: 2.0011293s"}