2024-10-28 15:35:21 -04:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2024-10-29 05:11:14 -04:00
|
|
|
func NewLogger(level string) *zap.Logger {
|
2024-10-28 15:35:21 -04:00
|
|
|
var logLevel zapcore.Level
|
2024-10-29 05:11:14 -04:00
|
|
|
switch strings.ToLower(level) {
|
2024-10-28 15:35:21 -04:00
|
|
|
case "info":
|
|
|
|
logLevel = zap.InfoLevel
|
|
|
|
case "error":
|
|
|
|
logLevel = zap.ErrorLevel
|
|
|
|
case "warn":
|
|
|
|
logLevel = zap.WarnLevel
|
|
|
|
case "debug":
|
|
|
|
logLevel = zap.DebugLevel
|
|
|
|
case "panic":
|
|
|
|
logLevel = zap.PanicLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
encoderConfig := zap.NewProductionEncoderConfig()
|
|
|
|
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
|
|
|
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
|
|
|
|
consoleWriter := zapcore.AddSync(os.Stdout)
|
|
|
|
consoleCore := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), consoleWriter, logLevel)
|
2024-10-29 05:11:14 -04:00
|
|
|
logger := zap.New(consoleCore)
|
|
|
|
return logger
|
2024-10-28 15:35:21 -04:00
|
|
|
}
|