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

34 lines
997 B
Go

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
})
consoleDebugging := zapcore.Lock(os.Stdout)
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
}