This commit is contained in:
crusader 2018-03-21 19:04:29 +09:00
parent 2a89a3c441
commit 71ae3a62af
2 changed files with 32 additions and 11 deletions

View File

@ -8,31 +8,38 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
var logger *zap.Logger var logger *zap.SugaredLogger
func Logger() *zap.Logger { func Logger() *zap.SugaredLogger {
if nil == logger { if nil == logger {
log.Panicf("Initialize Logger first to use Logger") log.Panicf("Initialize Logger first to use Logger")
} }
return logger return logger
} }
func InitializeLogger(configPath string) { func InitializeLogger(configPath string) error {
var cfg zap.Config var (
var err error cfg zap.Config
_logger *zap.Logger
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) return err
} }
logger.Info("Logger has been initialized as development mode") logger = _logger.Sugar()
return logger.Infof("Logger has been initialized as development mode")
return nil
} }
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) return err
} }
logger = _logger.Sugar()
return nil
} }

14
logging_test.go Normal file
View File

@ -0,0 +1,14 @@
package logging
import (
"log"
"testing"
)
func TestInitializeLogger(t *testing.T) {
if err := InitializeLogger(""); nil != err {
log.Panicf("Failed %v", err)
}
logger.Infof("Logger has been initialized")
}