Restructure project
This commit is contained in:
parent
e44e7caa11
commit
16b2f17301
46 changed files with 1744 additions and 1265 deletions
44
internal/logging/logger_test.go
Normal file
44
internal/logging/logger_test.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInfoLogger(t *testing.T) {
|
||||
level := "info"
|
||||
log := CreateLogger(level)
|
||||
defer log.Sync()
|
||||
|
||||
assert.NotEmpty(t, log, "Logger should not be nil")
|
||||
assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level))
|
||||
}
|
||||
|
||||
func TestWarnLogger(t *testing.T) {
|
||||
level := "warn"
|
||||
log := CreateLogger(level)
|
||||
defer log.Sync()
|
||||
|
||||
assert.NotEmpty(t, log, "Logger should not be nil")
|
||||
assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level))
|
||||
}
|
||||
|
||||
func TestDebugLogger(t *testing.T) {
|
||||
level := "debug"
|
||||
log := CreateLogger(level)
|
||||
defer log.Sync()
|
||||
|
||||
assert.NotEmpty(t, log, "Logger should not be nil")
|
||||
assert.Equal(t, log.Level().String(), level, fmt.Sprintf("Level should be %s", level))
|
||||
}
|
||||
|
||||
func TestInvalidLogger(t *testing.T) {
|
||||
level := "invalid"
|
||||
log := CreateLogger(level)
|
||||
defer log.Sync()
|
||||
|
||||
assert.NotEmpty(t, log, "Logger should not be nil")
|
||||
assert.Equal(t, log.Level().String(), "info", "Level should be info")
|
||||
}
|
33
internal/logging/logging.go
Normal file
33
internal/logging/logging.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package logging
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func CreateLogger(logLevel string) *zap.Logger {
|
||||
encoderCfg := zap.NewProductionEncoderConfig()
|
||||
encoderCfg.TimeKey = "time"
|
||||
encoderCfg.EncodeTime = zapcore.TimeEncoderOfLayout(time.StampMilli)
|
||||
|
||||
level := zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
zapLevel, err := zap.ParseAtomicLevel(logLevel)
|
||||
if err == nil {
|
||||
level = zapLevel
|
||||
}
|
||||
|
||||
config := zap.Config{
|
||||
Level: level,
|
||||
Development: false,
|
||||
DisableCaller: false,
|
||||
DisableStacktrace: false,
|
||||
Sampling: nil,
|
||||
Encoding: "json",
|
||||
EncoderConfig: encoderCfg,
|
||||
OutputPaths: []string{"stdout"},
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
}
|
||||
return zap.Must(config.Build())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue