This commit is contained in:
crusader 2018-03-21 18:32:10 +09:00
parent 3a7b7e4536
commit 2a89a3c441

View File

@ -1,8 +1,6 @@
package logging package logging
import ( import (
"flag"
"fmt"
"log" "log"
"git.loafle.net/commons_go/config" "git.loafle.net/commons_go/config"
@ -10,33 +8,22 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
var (
configPath *string
)
func init() {
configPath = flag.String("log-config-path", "./logging.json", "The path of logger config file")
flag.Parse()
}
var logger *zap.Logger var logger *zap.Logger
func Logger() *zap.Logger { func Logger() *zap.Logger {
if nil == logger { if nil == logger {
initializeLogger() log.Panicf("Initialize Logger first to use Logger")
logger.Info(fmt.Sprintf("Logger has been initialized. config[%s]", *configPath))
} }
return logger return logger
} }
func initializeLogger() { func InitializeLogger(configPath string) {
var cfg zap.Config var cfg zap.Config
var err error var err error
conf := config.New() 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) log.Printf("Loading of logger config has been failed [%v]", err)
if logger, err = zap.NewDevelopment(); nil != err { if logger, err = zap.NewDevelopment(); nil != err {
log.Panicf("Building of logger config[Development] has been failed [%v]", err) log.Panicf("Building of logger config[Development] has been failed [%v]", err)
@ -46,6 +33,6 @@ func initializeLogger() {
} }
if logger, err = cfg.Build(); nil != err { 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)
} }
} }