ing
This commit is contained in:
parent
cc5ba36df8
commit
5db5cb3f2d
21
logging.go
21
logging.go
|
@ -1,6 +1,7 @@
|
|||
package logging
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
|
||||
"git.loafle.net/commons_go/config"
|
||||
|
@ -8,31 +9,39 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
configPath *string
|
||||
)
|
||||
|
||||
func init() {
|
||||
configPath = flag.String("logger-config-path", "./logging.json", "The path of logger config file")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
var logger *zap.Logger
|
||||
|
||||
func Logger() *zap.Logger {
|
||||
if nil == logger {
|
||||
log.Panicf("Logger is not initialized")
|
||||
initializeLogger()
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
func InitializeLogger(configPath string) {
|
||||
func initializeLogger() {
|
||||
var cfg zap.Config
|
||||
var err error
|
||||
|
||||
conf := config.New()
|
||||
|
||||
if err = conf.Load(&cfg, configPath); nil != err {
|
||||
if err = conf.Load(&cfg, *configPath); nil != err {
|
||||
log.Printf("Loading of logger config has been failed [%v]", err)
|
||||
if logger, err = zap.NewDevelopment(); nil != err {
|
||||
log.Panicf("Building of logger configp[Development] has been failed [%v]", err)
|
||||
log.Panicf("Building of logger config[Development] has been failed [%v]", err)
|
||||
}
|
||||
log.Printf("Logger has been initialized as development mode")
|
||||
return
|
||||
}
|
||||
|
||||
if logger, err = cfg.Build(); nil != err {
|
||||
log.Panicf("Building of logger config[%s] has been failed [%v]", configPath, err)
|
||||
log.Panicf("Building of logger config[%s] has been failed [%v]", *configPath, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user