1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 13:24:42 -05:00
sub2clash/logger/logger.go
Nite07 effd22c750 ♻️ Refactor logger
📝 Update README
2024-04-24 13:01:22 +08:00

53 lines
1.2 KiB
Go

package logger
import (
"os"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var Logger *zap.Logger
func InitLogger(logLevel string) {
logger := zap.New(buildZapCore(getZapLogLevel(logLevel)))
Logger = logger
}
func buildZapCore(logLevel zapcore.Level) zapcore.Core {
fileWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: "logs/app.log",
MaxSize: 500,
MaxBackups: 3,
MaxAge: 28,
Compress: true,
})
consoleWriter := zapcore.AddSync(os.Stdout)
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
fileCore := zapcore.NewCore(zapcore.NewJSONEncoder(encoderConfig), fileWriter, logLevel)
consoleCore := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConfig), consoleWriter, logLevel)
combinedCore := zapcore.NewTee(fileCore, consoleCore)
return combinedCore
}
func getZapLogLevel(logLevel string) zapcore.Level {
switch strings.ToLower(logLevel) {
case "debug":
return zap.DebugLevel
case "warn":
return zap.WarnLevel
case "error":
return zap.ErrorLevel
case "info":
return zap.InfoLevel
default:
return zap.InfoLevel
}
}