加入网络层

This commit is contained in:
2025-06-26 23:57:54 +08:00
parent 53106465ed
commit 0f29dccec4
57 changed files with 1859 additions and 1274 deletions

View File

@@ -1,47 +1,48 @@
package log
import (
commonConfig "common/config"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
func Init(cfg *commonConfig.LoggerConfig) {
func Init(debug bool, maxSize, maxBackups, maxAge int, level string) {
// 格式配置
jsonConfig := zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
FunctionKey: zapcore.OmitKey,
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("01-02 15:04:05.000"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: nil,
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
FunctionKey: zapcore.OmitKey,
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout("01-02 15:04:05.000"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: func(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + loggerName + "]")
},
ConsoleSeparator: " ",
}
// 日志输出到控制台和文件
writeSyncer := []zapcore.WriteSyncer{zapcore.AddSync(os.Stdout)}
if !cfg.Debug {
if !debug {
writeSyncer = append(writeSyncer, zapcore.AddSync(&lumberjack.Logger{
Filename: "./logs/log.log", // 日志文件位置
MaxSize: cfg.MaxSize, // 最大文件大小MB
MaxBackups: cfg.MaxBackUp, // 保留旧文件的最大个数
MaxAge: cfg.MaxAge, // 保留旧文件的最大天数
MaxSize: maxSize, // 最大文件大小MB
MaxBackups: maxBackups, // 保留旧文件的最大个数
MaxAge: maxAge, // 保留旧文件的最大天数
Compress: false, // 是否压缩/归档旧文件
LocalTime: true,
}))
}
var encoder zapcore.Encoder
if cfg.Debug {
if debug {
encoder = zapcore.NewConsoleEncoder(jsonConfig)
} else {
encoder = zapcore.NewJSONEncoder(jsonConfig)
@@ -49,9 +50,9 @@ func Init(cfg *commonConfig.LoggerConfig) {
logger := zap.New(zapcore.NewCore(
encoder,
zapcore.NewMultiWriteSyncer(writeSyncer...),
zap.NewAtomicLevelAt(GetLogLevel(cfg.Level)),
zap.NewAtomicLevelAt(GetLogLevel(level)),
))
if cfg.Debug {
if debug {
logger = logger.WithOptions(
zap.AddCaller(),
zap.AddCallerSkip(1),