logging/logging.go

46 lines
800 B
Go
Raw Permalink 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
)
2018-03-21 10:04:29 +00:00
var logger *zap.SugaredLogger
2017-08-30 04:03:03 +00:00
2018-03-21 10:04:29 +00:00
func Logger() *zap.SugaredLogger {
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 10:04:29 +00:00
func InitializeLogger(configPath string) error {
var (
cfg zap.Config
_logger *zap.Logger
err error
)
2017-09-19 06:00:20 +00:00
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)
2018-03-21 10:04:29 +00:00
if _logger, err = zap.NewDevelopment(); nil != err {
return err
2017-11-09 06:09:40 +00:00
}
2018-03-21 10:04:29 +00:00
logger = _logger.Sugar()
logger.Infof("Logger has been initialized as development mode")
return nil
2017-08-30 04:03:03 +00:00
}
2017-09-11 03:42:00 +00:00
2018-03-21 10:04:29 +00:00
if _logger, err = cfg.Build(); nil != err {
return err
2017-09-11 03:42:00 +00:00
}
2018-03-21 10:04:29 +00:00
logger = _logger.Sugar()
return nil
2017-08-30 04:03:03 +00:00
}