serverctl/services/entry/pkg/infrastructure/dependencies/logger.go

37 lines
1006 B
Go
Raw Normal View History

2022-02-16 16:27:48 +01:00
package dependencies
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io/ioutil"
"os"
)
func setupLogger() *zap.Logger {
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
fileDebugging := zapcore.AddSync(ioutil.Discard)
fileErrors := zapcore.AddSync(ioutil.Discard)
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
fileEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
_ = zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, fileErrors, highPriority),
zapcore.NewCore(fileEncoder, consoleErrors, highPriority),
zapcore.NewCore(fileEncoder, fileDebugging, lowPriority),
zapcore.NewCore(fileEncoder, consoleDebugging, lowPriority),
)
logger := zap.New(core)
defer logger.Sync()
return logger
}