Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkuroki committed Oct 31, 2020
1 parent 9fb8fc6 commit 7091a99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
9 changes: 7 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ func GetLogger() *Logger {
return logger
}

// SetLogger sets logger
func SetLogger(log *Logger) {
logger = log
}

// GetZapLogger returns zapSugaredLogger
func GetZapLogger() *zap.SugaredLogger {
return logger.zap
}

// newLogger create logger object for *gorm.DB from *echo.Logger
func newLogger(zap *zap.SugaredLogger) *Logger {
func NewLogger(zap *zap.SugaredLogger) *Logger {
return &Logger{zap: zap}
}

Expand All @@ -52,7 +57,7 @@ func InitLogger() {
}
sugar := zap.Sugar()
// set package varriable logger.
logger = newLogger(sugar)
logger = NewLogger(sugar)
logger.zap.Infof("Success to read zap logger configuration: zaplogger." + *config.GetEnv() + ".yml")
_ = zap.Sync()
}
Expand Down
39 changes: 38 additions & 1 deletion test/unittest_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package test

import (
"encoding/json"
"fmt"

"github.com/labstack/echo/v4"
"github.com/ybkuroki/go-webapp-sample/config"
"github.com/ybkuroki/go-webapp-sample/logger"
"github.com/ybkuroki/go-webapp-sample/middleware"
"github.com/ybkuroki/go-webapp-sample/migration"
"github.com/ybkuroki/go-webapp-sample/repository"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Prepare func is to prepare for unit test.
Expand All @@ -24,7 +27,7 @@ func Prepare() *echo.Echo {
conf.Log.RequestLogFormat = "${remote_ip} ${account_name} ${uri} ${method} ${status}"
config.SetConfig(conf)

logger.InitLogger()
initTestLogger()
middleware.InitLoggerMiddleware(e)

repository.InitDB()
Expand All @@ -36,6 +39,40 @@ func Prepare() *echo.Echo {
return e
}

func initTestLogger() {
level := zap.NewAtomicLevel()
level.SetLevel(zapcore.DebugLevel)

myConfig := zap.Config{
Level: level,
Encoding: "console",
Development: true,
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "Time",
LevelKey: "Level",
NameKey: "Name",
CallerKey: "Caller",
MessageKey: "Msg",
StacktraceKey: "St",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
zap, err := myConfig.Build()
if err != nil {
fmt.Printf("Error")
}
sugar := zap.Sugar()
log := logger.NewLogger(sugar)
logger.SetLogger(log)
logger.GetZapLogger().Infof("Success to construct zap logger.")
_ = zap.Sync()
}

// ConvertToString func is convert model to string.
func ConvertToString(model interface{}) string {
bytes, _ := json.Marshal(model)
Expand Down

0 comments on commit 7091a99

Please sign in to comment.