Inspired by a popular NodeJS logger library: debug.
- zero configuration for most use cases.
- Colorful console output
- Enable logger by environment easily
go get github.com/joway/loki@latest
It will not been affected by LOKI_ENV
.
loki.SetLevel(loki.INFO)
loki.Info("x: %s", "hi")
loki.Debug("x: %s", "hi")
loki.Error("x: %s", "hi")
logger := loki.New("app:xxx")
logger.Info("x: %s", "hi")
logger.Debug("x: %s", "hi")
logger.Error("x: %s", "hi")
To enable the logger, just add env LOKI_ENV=app:xxx
in your command, like: LOKI_ENV=app:xxx ./main
.
You can also use LOKI_ENV=app:a,app:b,model:a
to enable multiple logger.
LOKI_ENV=*
can enable all loggers.
// "02 Jan 06 15:04 MST"
l := loki.New("app:xxx")
fp, err := os.OpenFile("test.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer fp.Close()
flushIntervalMs := 1000
l.SetHandler(loki.NewFileHandler(fp, flushIntervalMs))
l.Info("x: %s", "hi")
// "02 Jan 06 15:04 MST"
loki.SetTimeFormatter(time.RFC822)
// disable time output
loki.SetTimeFormatter("")
type ErrFormatter struct {
loki.Formatter
}
func (f ErrFormatter) format(a ...interface{}) string {
err := a[0].(error)
return fmt.Sprintf("Error %v", err)
}
logger := loki.New("app:xxx")
f := ErrFormatter{}
logger.SetFormatter(f)
logger.Debug(errors.New("test error"))