logging/logging.go

48 lines
867 B
Go
Raw Normal View History

2017-08-30 04:03:03 +00:00
package logging
import (
2018-03-20 14:55:46 +00:00
"flag"
2018-03-20 14:44:19 +00:00
"log"
2017-09-11 03:42:00 +00:00
"git.loafle.net/commons_go/config"
2017-08-30 04:03:03 +00:00
2017-08-30 05:00:22 +00:00
"go.uber.org/zap"
2017-08-30 04:03:03 +00:00
)
2018-03-20 14:55:46 +00:00
var (
configPath *string
)
func init() {
configPath = flag.String("logger-config-path", "./logging.json", "The path of logger config file")
flag.Parse()
}
2017-11-09 06:09:40 +00:00
var logger *zap.Logger
2017-08-30 04:03:03 +00:00
2017-11-09 06:09:40 +00:00
func Logger() *zap.Logger {
2017-11-09 06:09:57 +00:00
if nil == logger {
2018-03-20 14:55:46 +00:00
initializeLogger()
2017-11-09 06:09:40 +00:00
}
return logger
}
2018-03-20 14:55:46 +00:00
func initializeLogger() {
2017-09-19 06:00:20 +00:00
var cfg zap.Config
var err error
2017-09-11 03:42:00 +00:00
conf := config.New()
2017-08-30 04:03:03 +00:00
2018-03-20 14:55:46 +00:00
if err = conf.Load(&cfg, *configPath); nil != err {
2018-03-20 14:44:19 +00:00
log.Printf("Loading of logger config has been failed [%v]", err)
2017-11-09 06:09:40 +00:00
if logger, err = zap.NewDevelopment(); nil != err {
2018-03-20 14:55:46 +00:00
log.Panicf("Building of logger config[Development] has been failed [%v]", err)
2017-11-09 06:09:40 +00:00
}
return
2017-08-30 04:03:03 +00:00
}
2017-09-11 03:42:00 +00:00
2017-11-09 06:09:40 +00:00
if logger, err = cfg.Build(); nil != err {
2018-03-20 14:55:46 +00:00
log.Panicf("Building of logger config[%s] has been failed [%v]", *configPath, err)
2017-09-11 03:42:00 +00:00
}
2017-08-30 04:03:03 +00:00
}