ing
This commit is contained in:
parent
2a89a3c441
commit
71ae3a62af
29
logging.go
29
logging.go
|
@ -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
14
logging_test.go
Normal 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")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user