mirror of
https://github.com/nitezs/sub2clash.git
synced 2024-12-23 10:34:42 -05:00
♻️ Refactor logger
📝 Update README
This commit is contained in:
parent
566965bb6a
commit
effd22c750
@ -5,4 +5,3 @@ REQUEST_RETRY_TIMES=3
|
||||
REQUEST_MAX_FILE_SIZE=1048576
|
||||
CACHE_EXPIRE=300
|
||||
LOG_LEVEL=info
|
||||
BASE_PATH=/
|
||||
|
@ -42,12 +42,12 @@
|
||||
|
||||
### API
|
||||
|
||||
[API 文档](./API_README.md)
|
||||
[API 文档](./API.md)
|
||||
|
||||
### 模板
|
||||
|
||||
可以通过变量自定义模板中的策略组代理节点
|
||||
解释的不太清楚,可以参考下方默认模板
|
||||
具体参考下方默认模板
|
||||
|
||||
- `<all>` 为添加所有节点
|
||||
- `<countries>` 为添加所有国家策略组
|
||||
@ -57,7 +57,3 @@
|
||||
|
||||
- [Clash](./templates/template_clash.yaml)
|
||||
- [Clash.Meta](./templates/template_meta.yaml)
|
||||
|
||||
## 已知问题
|
||||
|
||||
[代理链接解析](./parser)还没有经过严格测试,可能会出现解析错误的情况,如果出现问题请提交 issue
|
||||
|
1
go.mod
1
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
port: 7890
|
||||
socks-port: 7891
|
||||
mixed-port: 7890
|
||||
allow-lan: true
|
||||
mode: Rule
|
||||
log-level: info
|
||||
|
@ -1,5 +1,4 @@
|
||||
port: 7890
|
||||
socks-port: 7891
|
||||
mixed-port: 7890
|
||||
allow-lan: true
|
||||
mode: Rule
|
||||
log-level: info
|
||||
|
Loading…
Reference in New Issue
Block a user