2024-01-15 15:02:58 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2024-02-24 13:41:40 +00:00
|
|
|
"os"
|
|
|
|
|
2024-01-15 15:02:58 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func NewFallbackLogger() *zap.Logger {
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2024-02-24 13:41:40 +00:00
|
|
|
func NewLogger() (*zap.Logger, *zap.AtomicLevel) {
|
2024-01-15 15:02:58 +00:00
|
|
|
|
2024-01-16 00:43:00 +00:00
|
|
|
// Create a new atomic level
|
|
|
|
atomicLevel := zap.NewAtomicLevel()
|
2024-01-15 15:02:58 +00:00
|
|
|
|
2024-01-16 00:43:00 +00:00
|
|
|
// Set initial log level, for example, info level
|
|
|
|
atomicLevel.SetLevel(mapLogLevel(viper.GetString("core.log.level")))
|
2024-01-15 15:02:58 +00:00
|
|
|
|
2024-01-16 00:43:00 +00:00
|
|
|
// Create the logger with the atomic level
|
|
|
|
logger := zap.New(zapcore.NewCore(
|
|
|
|
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
|
|
|
zapcore.Lock(os.Stdout),
|
|
|
|
atomicLevel,
|
|
|
|
))
|
2024-01-15 15:02:58 +00:00
|
|
|
|
2024-02-24 13:41:40 +00:00
|
|
|
return logger, &atomicLevel
|
2024-01-15 15:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mapLogLevel(level string) zapcore.Level {
|
|
|
|
switch level {
|
|
|
|
case "debug":
|
|
|
|
return zapcore.DebugLevel
|
|
|
|
case "info":
|
|
|
|
return zapcore.InfoLevel
|
|
|
|
case "warn":
|
|
|
|
return zapcore.WarnLevel
|
|
|
|
default:
|
|
|
|
return zapcore.ErrorLevel
|
|
|
|
}
|
|
|
|
}
|