From effd22c7506fc19074c51dc3ae08468cc7d89b9a Mon Sep 17 00:00:00 2001 From: Nite07 Date: Wed, 24 Apr 2024 13:01:22 +0800 Subject: [PATCH] :recycle: Refactor logger :memo: Update README --- .env.example | 1 - API_README.md => API.md | 0 README.md | 8 +--- go.mod | 1 + go.sum | 2 + logger/logger.go | 88 ++++++++++++++--------------------- templates/template_clash.yaml | 3 +- templates/template_meta.yaml | 3 +- 8 files changed, 42 insertions(+), 64 deletions(-) rename API_README.md => API.md (100%) diff --git a/.env.example b/.env.example index 6a8ea40..e6f415d 100644 --- a/.env.example +++ b/.env.example @@ -5,4 +5,3 @@ REQUEST_RETRY_TIMES=3 REQUEST_MAX_FILE_SIZE=1048576 CACHE_EXPIRE=300 LOG_LEVEL=info -BASE_PATH=/ diff --git a/API_README.md b/API.md similarity index 100% rename from API_README.md rename to API.md diff --git a/README.md b/README.md index 5e85def..a58c7fb 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,12 @@ ### API -[API 文档](./API_README.md) +[API 文档](./API.md) ### 模板 可以通过变量自定义模板中的策略组代理节点 -解释的不太清楚,可以参考下方默认模板 +具体参考下方默认模板 - `` 为添加所有节点 - `` 为添加所有国家策略组 @@ -57,7 +57,3 @@ - [Clash](./templates/template_clash.yaml) - [Clash.Meta](./templates/template_meta.yaml) - -## 已知问题 - -[代理链接解析](./parser)还没有经过严格测试,可能会出现解析错误的情况,如果出现问题请提交 issue diff --git a/go.mod b/go.mod index a174e98..a00c25f 100644 --- a/go.mod +++ b/go.mod @@ -37,4 +37,5 @@ require ( golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect google.golang.org/protobuf v1.33.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect ) diff --git a/go.sum b/go.sum index 98cb763..004c9ab 100644 --- a/go.sum +++ b/go.sum @@ -93,6 +93,8 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logger/logger.go b/logger/logger.go index 5dcffe9..e2d5742 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,70 +1,52 @@ package logger import ( - "path/filepath" - "sync" - "time" + "os" + "strings" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" ) -var ( - Logger *zap.Logger - lock sync.Mutex - logLevel string -) +var Logger *zap.Logger -func InitLogger(level string) { - logLevel = level - buildLogger() - go rotateLogs() +func InitLogger(logLevel string) { + logger := zap.New(buildZapCore(getZapLogLevel(logLevel))) + Logger = logger } -func buildLogger() { - lock.Lock() - defer lock.Unlock() - var level zapcore.Level - switch logLevel { - case "error": - level = zap.ErrorLevel +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": - level = zap.DebugLevel + return zap.DebugLevel case "warn": - level = zap.WarnLevel + return zap.WarnLevel + case "error": + return zap.ErrorLevel case "info": - level = zap.InfoLevel + return 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 - Logger, err = zapConfig.Build() - if err != nil { - panic("log failed" + err.Error()) - } -} - -// 根据日期获得日志文件 -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() + return zap.InfoLevel } } diff --git a/templates/template_clash.yaml b/templates/template_clash.yaml index 40748e6..306890d 100644 --- a/templates/template_clash.yaml +++ b/templates/template_clash.yaml @@ -1,5 +1,4 @@ -port: 7890 -socks-port: 7891 +mixed-port: 7890 allow-lan: true mode: Rule log-level: info diff --git a/templates/template_meta.yaml b/templates/template_meta.yaml index 5d47b72..e0d3972 100644 --- a/templates/template_meta.yaml +++ b/templates/template_meta.yaml @@ -1,5 +1,4 @@ -port: 7890 -socks-port: 7891 +mixed-port: 7890 allow-lan: true mode: Rule log-level: info