forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging.go
28 lines (24 loc) · 879 Bytes
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package mainboilerplate
import (
log "github.com/sirupsen/logrus"
)
// LogConfig configures handling of application log events.
type LogConfig struct {
Level string `long:"level" env:"LEVEL" default:"info" choice:"info" choice:"debug" choice:"warn" description:"Logging level"`
Format string `long:"format" env:"FORMAT" default:"text" choice:"json" choice:"text" choice:"color" description:"Logging output format"`
}
// InitLog configures the logger.
func InitLog(cfg LogConfig) {
if cfg.Format == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else if cfg.Format == "text" {
log.SetFormatter(&log.TextFormatter{})
} else if cfg.Format == "color" {
log.SetFormatter(&log.TextFormatter{ForceColors: true})
}
if lvl, err := log.ParseLevel(cfg.Level); err != nil {
log.WithField("err", err).Fatal("unrecognized log level")
} else {
log.SetLevel(lvl)
}
}