1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 12:44:42 -05:00
sub2clash/logger/logger.go

71 lines
1.5 KiB
Go
Raw Normal View History

2023-09-13 01:47:22 -04:00
package logger
import (
"path/filepath"
"sync"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
2023-09-13 01:47:22 -04:00
)
var (
Logger *zap.Logger
lock sync.Mutex
logLevel string
2023-09-13 01:47:22 -04:00
)
func InitLogger(level string) {
logLevel = level
2023-09-13 01:47:22 -04:00
buildLogger()
go rotateLogs()
}
func buildLogger() {
lock.Lock()
defer lock.Unlock()
var level zapcore.Level
switch logLevel {
2023-09-13 01:47:22 -04:00
case "error":
level = zap.ErrorLevel
case "debug":
level = zap.DebugLevel
case "warn":
level = zap.WarnLevel
case "info":
level = zap.InfoLevel
default:
level = zap.InfoLevel
}
zapConfig := zap.NewProductionConfig()
zapConfig.Encoding = "console"
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
zapConfig.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
zapConfig.OutputPaths = []string{"stdout", getLogFileName("info")}
zapConfig.ErrorOutputPaths = []string{"stderr", getLogFileName("error")}
zapConfig.Level = zap.NewAtomicLevelAt(level)
var err error
2023-09-13 01:47:22 -04:00
Logger, err = zapConfig.Build()
if err != nil {
panic("log failed" + err.Error())
2023-09-13 01:47:22 -04:00
}
}
// 根据日期获得日志文件
func getLogFileName(name string) string {
return filepath.Join("logs", time.Now().Format("2006-01-02")+"-"+name+".log")
}
func rotateLogs() {
for {
now := time.Now()
nextMidnight := time.Date(
now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location(),
).Add(24 * time.Hour)
durationUntilMidnight := nextMidnight.Sub(now)
time.Sleep(durationUntilMidnight)
buildLogger()
}
}