downloader/api/internal/app/infrastructure/logger/logger.go

34 lines
997 B
Go
Raw Permalink Normal View History

2021-12-22 01:22:33 +01:00
package logger
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
func New() *zap.SugaredLogger {
consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
jsonEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
2021-12-26 00:02:51 +01:00
consoleDebugging := zapcore.Lock(os.Stdout)
2021-12-22 01:22:33 +01:00
consoleErrors := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(jsonEncoder, consoleErrors, highPriority),
zapcore.NewCore(jsonEncoder, consoleDebugging, highPriority),
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logger := zap.New(core)
defer func(logger *zap.Logger) {
_ = logger.Sync()
}(logger)
sugaredLogger := logger.Sugar()
return sugaredLogger
}