logging/logging.go

39 lines
812 B
Go
Raw Normal View History

2017-08-30 04:03:03 +00:00
package logging
import (
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
)
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-21 09:32:10 +00:00
log.Panicf("Initialize Logger first to use Logger")
2017-11-09 06:09:40 +00:00
}
return logger
}
2018-03-21 09:32:10 +00:00
func InitializeLogger(configPath string) {
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-21 09:32:10 +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
}
2018-03-20 15:16:19 +00:00
logger.Info("Logger has been initialized as development mode")
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-21 09:32:10 +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
}