1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 14:34:41 -05:00

♻️ Refactor logger

📝 Update README
This commit is contained in:
Nite07 2024-04-24 13:01:22 +08:00
parent 566965bb6a
commit effd22c750
8 changed files with 42 additions and 64 deletions

View File

@ -5,4 +5,3 @@ REQUEST_RETRY_TIMES=3
REQUEST_MAX_FILE_SIZE=1048576 REQUEST_MAX_FILE_SIZE=1048576
CACHE_EXPIRE=300 CACHE_EXPIRE=300
LOG_LEVEL=info LOG_LEVEL=info
BASE_PATH=/

View File

@ -42,12 +42,12 @@
### API ### API
[API 文档](./API_README.md) [API 文档](./API.md)
### 模板 ### 模板
可以通过变量自定义模板中的策略组代理节点 可以通过变量自定义模板中的策略组代理节点
解释的不太清楚,可以参考下方默认模板 具体参考下方默认模板
- `<all>` 为添加所有节点 - `<all>` 为添加所有节点
- `<countries>` 为添加所有国家策略组 - `<countries>` 为添加所有国家策略组
@ -57,7 +57,3 @@
- [Clash](./templates/template_clash.yaml) - [Clash](./templates/template_clash.yaml)
- [Clash.Meta](./templates/template_meta.yaml) - [Clash.Meta](./templates/template_meta.yaml)
## 已知问题
[代理链接解析](./parser)还没有经过严格测试,可能会出现解析错误的情况,如果出现问题请提交 issue

1
go.mod
View File

@ -37,4 +37,5 @@ require (
golang.org/x/net v0.22.0 // indirect golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
) )

2
go.sum
View File

@ -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= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,70 +1,52 @@
package logger package logger
import ( import (
"path/filepath" "os"
"sync" "strings"
"time"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
) )
var ( var Logger *zap.Logger
Logger *zap.Logger
lock sync.Mutex
logLevel string
)
func InitLogger(level string) { func InitLogger(logLevel string) {
logLevel = level logger := zap.New(buildZapCore(getZapLogLevel(logLevel)))
buildLogger() Logger = logger
go rotateLogs()
} }
func buildLogger() { func buildZapCore(logLevel zapcore.Level) zapcore.Core {
lock.Lock() fileWriter := zapcore.AddSync(&lumberjack.Logger{
defer lock.Unlock() Filename: "logs/app.log",
var level zapcore.Level MaxSize: 500,
switch logLevel { MaxBackups: 3,
case "error": MaxAge: 28,
level = zap.ErrorLevel 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": case "debug":
level = zap.DebugLevel return zap.DebugLevel
case "warn": case "warn":
level = zap.WarnLevel return zap.WarnLevel
case "error":
return zap.ErrorLevel
case "info": case "info":
level = zap.InfoLevel return zap.InfoLevel
default: default:
level = zap.InfoLevel return 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()
} }
} }

View File

@ -1,5 +1,4 @@
port: 7890 mixed-port: 7890
socks-port: 7891
allow-lan: true allow-lan: true
mode: Rule mode: Rule
log-level: info log-level: info

View File

@ -1,5 +1,4 @@
port: 7890 mixed-port: 7890
socks-port: 7891
allow-lan: true allow-lan: true
mode: Rule mode: Rule
log-level: info log-level: info