ws logger支持通过配置控制

This commit is contained in:
白茶清欢 2025-04-25 18:46:36 +08:00
parent f5d7f54a55
commit 77bf0b2a00
2 changed files with 38 additions and 21 deletions

View File

@ -9,14 +9,13 @@ package logger
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/websocket/storage"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
@ -224,19 +223,22 @@ func defaultEncodeDuration(d time.Duration, enc zapcore.PrimitiveArrayEncoder) {
// //
// Date : 11:41 下午 2021/1/2 // Date : 11:41 下午 2021/1/2
type OptionLogger struct { type OptionLogger struct {
UseJsonFormat bool // 日志使用json格式 UseJsonFormat bool // 日志使用json格式
MessageKey string // message 字段 MessageKey string // message 字段
LevelKey string // level 字段 LevelKey string // level 字段
TimeKey string // 时间字段 TimeKey string // 时间字段
CallerKey string // 记录日志的文件的代码行数 CallerKey string // 记录日志的文件的代码行数
UseShortCaller bool // 使用短的调用文件格式 UseShortCaller bool // 使用短的调用文件格式
TimeEncoder zapcore.TimeEncoder // 格式化时间的函数 TimeEncoder zapcore.TimeEncoder // 格式化时间的函数
EncodeDuration zapcore.DurationEncoder // 原始时间信息 EncodeDuration zapcore.DurationEncoder // 原始时间信息
WithCaller bool // 是否打印文件行号 WithCaller bool // 是否打印文件行号
WithCallerSkip int // 跳过的调用数 WithCallerSkip int // 跳过的调用数
ConsoleOutput bool // 控制台输出 ConsoleOutput bool // 控制台输出
Encoder zapcore.Encoder // 编码函数 Encoder zapcore.Encoder // 编码函数
WsLoggerConnect storage.IConnection `json:"-"` // ws 日志连接管理实例 WsServerProt int // ws端口
WsGroup string // ws分组
GinRouter *gin.Engine // gin路由实例
EnableWsLog bool // 启用wsLog
} }
// SetLoggerOptionFunc 设置日志配置 // SetLoggerOptionFunc 设置日志配置
@ -417,3 +419,16 @@ func GetEncoder(option ...SetLoggerOptionFunc) zapcore.Encoder {
} }
return zapcore.NewJSONEncoder(ec) return zapcore.NewJSONEncoder(ec)
} }
// EnableWsLogger 启用wsLogger
func EnableWsLogger(serverPort int, wsGroup string, router *gin.Engine) SetLoggerOptionFunc {
return func(o *OptionLogger) {
o.GinRouter = router
o.WsServerProt = serverPort
if len(wsGroup) == 0 {
wsGroup = "ws-log"
}
o.WsGroup = wsGroup
o.EnableWsLog = true
}
}

View File

@ -64,12 +64,14 @@ func NewLogger(inputLoggerLevel consts.LogLevel, splitConfig *RotateLogConfig, o
// zapcore.NewCore(o.Encoder, zapcore.AddSync(&wsWriter{}), loggerLevelDeal), // 设置ws日志输出 // zapcore.NewCore(o.Encoder, zapcore.AddSync(&wsWriter{}), loggerLevelDeal), // 设置ws日志输出
} }
// TODO: 通过配置控制ws logger // 通过配置控制ws logger
if wsLoggerInstance, err := instance.NewWebsocketLog(80, nil); nil != err { if o.EnableWsLog {
return nil, err if wsLoggerInstance, err := instance.NewWebsocketLog(o.WsServerProt, o.GinRouter); nil != err {
} else { return nil, err
// 设置ws日志输出 } else {
fileHandlerList = append(fileHandlerList, zapcore.NewCore(o.Encoder, zapcore.AddSync(wsLoggerInstance.Writer()), loggerLevelDeal)) // 设置ws日志输出
fileHandlerList = append(fileHandlerList, zapcore.NewCore(o.Encoder, zapcore.AddSync(wsLoggerInstance.Writer()), loggerLevelDeal))
}
} }
// 设置控制台输出 // 设置控制台输出