ws logger 更改为全局注册
This commit is contained in:
		
							
								
								
									
										11
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								config.go
									
									
									
									
									
								
							| @ -237,17 +237,6 @@ type OptionLogger struct { | ||||
| // SetLoggerOptionFunc 设置日志配置 | ||||
| type SetLoggerOptionFunc func(o *OptionLogger) | ||||
|  | ||||
| // WithWsLogger 设置ws管理实例 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 22:11 2024/7/22 | ||||
| func WithWsLogger(connect storage.IConnection) SetLoggerOptionFunc { | ||||
| 	return func(o *OptionLogger) { | ||||
| 		o.WsLoggerConnect = connect | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // WithCaller 打开文件行号记录 | ||||
| func WithCaller() SetLoggerOptionFunc { | ||||
| 	return func(o *OptionLogger) { | ||||
|  | ||||
							
								
								
									
										52
									
								
								define.go
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								define.go
									
									
									
									
									
								
							| @ -18,28 +18,47 @@ import ( | ||||
| 	"go.uber.org/zap/zapcore" | ||||
| ) | ||||
|  | ||||
| var wsLoggerConnect storage.IConnection // ws 日志连接管理实例 | ||||
|  | ||||
| // SetWsLoggConnect 设置ws connect | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 15:20 2024/7/23 | ||||
| func SetWsLoggConnect(connect storage.IConnection) { | ||||
| 	wsLoggerConnect = connect | ||||
| } | ||||
|  | ||||
| // GetWsLoggConnect 获取ws连接 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 15:34 2024/7/23 | ||||
| func GetWsLoggConnect(connect storage.IConnection) { | ||||
| 	wsLoggerConnect = connect | ||||
| } | ||||
|  | ||||
| // InputLogConfig 输入的日志配置 | ||||
| // | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 18:40 2022/6/12 | ||||
| type InputLogConfig struct { | ||||
| 	Name             string              `json:"name" yaml:"name"`                             // 日志文件名 | ||||
| 	Path             string              `json:"path" yaml:"path"`                             // 日志文件路径 | ||||
| 	TimeIntervalType string              `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则 | ||||
| 	DivisionChar     string              `json:"division_char" yaml:"division_char"`           // 文件名分隔符 | ||||
| 	LogLevel         string              `json:"log_level" yaml:"log_level"`                   // 日志等级 | ||||
| 	Console          bool                `json:"console" yaml:"console"`                       // 是否进行控制台日志输出 | ||||
| 	UseJson          bool                `json:"use_json" yaml:"use_json"`                     // 日志是否使用JSON格式 | ||||
| 	FileLine         bool                `json:"file_line" yaml:"file_line"`                   // 日志是否打印行号 | ||||
| 	MessageKey       string              `json:"message_key" yaml:"message_key"`               // message 字段 | ||||
| 	LevelKey         string              `json:"level_key" yaml:"level_key"`                   // level 字段 | ||||
| 	TimeKey          string              `json:"time_key" yaml:"time_key"`                     // 时间字段 | ||||
| 	CallerKey        string              `json:"caller_key" yaml:"caller_key"`                 // 记录日志的文件的代码行数 | ||||
| 	UseShortFile     bool                `json:"use_short_file" yaml:"use_short_file"`         // 是否使用短文件格式 | ||||
| 	CallerSkip       int                 `json:"caller_skip" yaml:"caller_skip"`               // 日志记录的文件跳过多少层 | ||||
| 	MaxAge           int                 `json:"max_age" yaml:"max_age"`                       // 日志最长保存时间, 单位 : 秒 | ||||
| 	WsLoggerConnect  storage.IConnection `json:"-"`                                            // ws 日志连接管理实例 | ||||
| 	Name             string `json:"name" yaml:"name"`                             // 日志文件名 | ||||
| 	Path             string `json:"path" yaml:"path"`                             // 日志文件路径 | ||||
| 	TimeIntervalType string `json:"time_interval_type" yaml:"time_interval_type"` // 日志切割规则 | ||||
| 	DivisionChar     string `json:"division_char" yaml:"division_char"`           // 文件名分隔符 | ||||
| 	LogLevel         string `json:"log_level" yaml:"log_level"`                   // 日志等级 | ||||
| 	Console          bool   `json:"console" yaml:"console"`                       // 是否进行控制台日志输出 | ||||
| 	UseJson          bool   `json:"use_json" yaml:"use_json"`                     // 日志是否使用JSON格式 | ||||
| 	FileLine         bool   `json:"file_line" yaml:"file_line"`                   // 日志是否打印行号 | ||||
| 	MessageKey       string `json:"message_key" yaml:"message_key"`               // message 字段 | ||||
| 	LevelKey         string `json:"level_key" yaml:"level_key"`                   // level 字段 | ||||
| 	TimeKey          string `json:"time_key" yaml:"time_key"`                     // 时间字段 | ||||
| 	CallerKey        string `json:"caller_key" yaml:"caller_key"`                 // 记录日志的文件的代码行数 | ||||
| 	UseShortFile     bool   `json:"use_short_file" yaml:"use_short_file"`         // 是否使用短文件格式 | ||||
| 	CallerSkip       int    `json:"caller_skip" yaml:"caller_skip"`               // 日志记录的文件跳过多少层 | ||||
| 	MaxAge           int    `json:"max_age" yaml:"max_age"`                       // 日志最长保存时间, 单位 : 秒 | ||||
| } | ||||
|  | ||||
| // GetLogInstanceFromInputConfig 从输入配置获取日志实例 | ||||
| @ -56,7 +75,6 @@ func GetLogInstanceFromInputConfig(logConf *InputLogConfig) (*zap.Logger, error) | ||||
| 		WithCaller(), | ||||
| 		WithUseJsonFormat(logConf.UseJson), | ||||
| 		WithShortCaller(logConf.UseShortFile), | ||||
| 		WithWsLogger(logConf.WsLoggerConnect), | ||||
| 	} | ||||
| 	if logConf.Console { | ||||
| 		logConfList = append(logConfList, WithConsoleOutput()) | ||||
|  | ||||
| @ -11,8 +11,6 @@ import ( | ||||
| 	"io" | ||||
| 	"os" | ||||
|  | ||||
| 	"git.zhangdeman.cn/zhangdeman/websocket/storage" | ||||
|  | ||||
| 	"github.com/pkg/errors" | ||||
|  | ||||
| 	"go.uber.org/zap" | ||||
| @ -61,7 +59,7 @@ func NewLogger(inputLoggerLevel string, splitConfig *RotateLogConfig, optionFunc | ||||
|  | ||||
| 	fileHandlerList := []zapcore.Core{ | ||||
| 		zapcore.NewCore(o.Encoder, zapcore.AddSync(loggerWriter), loggerLevelDeal), | ||||
| 		zapcore.NewCore(o.Encoder, zapcore.AddSync(&wsWriter{o.WsLoggerConnect}), loggerLevelDeal), // 设置ws日志输出 | ||||
| 		zapcore.NewCore(o.Encoder, zapcore.AddSync(&wsWriter{}), loggerLevelDeal), // 设置ws日志输出 | ||||
| 	} | ||||
|  | ||||
| 	// 设置控制台输出 | ||||
| @ -141,7 +139,6 @@ func (l *Logger) getWriter() (io.Writer, error) { | ||||
| } | ||||
|  | ||||
| type wsWriter struct { | ||||
| 	connection storage.IConnection | ||||
| } | ||||
|  | ||||
| // Write ws的writer | ||||
| @ -150,11 +147,11 @@ type wsWriter struct { | ||||
| // | ||||
| // Date : 22:24 2024/7/22 | ||||
| func (w *wsWriter) Write(p []byte) (n int, err error) { | ||||
| 	if nil == w.connection { | ||||
| 	if nil == wsLoggerConnect { | ||||
| 		return 0, nil | ||||
| 	} | ||||
|  | ||||
| 	allConnList := w.connection.GetCtxList() | ||||
| 	allConnList := wsLoggerConnect.GetCtxList() | ||||
| 	for _, conn := range allConnList { | ||||
| 		_ = conn.Session.Write(p) | ||||
| 	} | ||||
|  | ||||
		Reference in New Issue
	
	Block a user