diff --git a/.gitignore b/.gitignore index 474488e..036efc7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ .idea +logs +.vscode diff --git a/abstract/IWebsocket.go b/abstract/IWebsocket.go index e17134e..6050b11 100644 --- a/abstract/IWebsocket.go +++ b/abstract/IWebsocket.go @@ -8,6 +8,7 @@ package abstract import ( + "github.com/go-developer/websocket/config" "github.com/go-developer/websocket/context" ) @@ -65,5 +66,5 @@ type IWebsocket interface { // Author : go_developer@163.com<张德满> // // Date : 7:01 下午 2021/4/17 - GetWSServerConfig() + GetWSServerConfig() []config.SetWSServerConfig } diff --git a/config/server.go b/config/server.go index 6e1fb15..f440df2 100644 --- a/config/server.go +++ b/config/server.go @@ -8,6 +8,7 @@ package config import ( + "github.com/go-developer/gopkg/logger" "go.uber.org/zap/zapcore" ) @@ -35,8 +36,8 @@ const ( DefaultMode = RunModeDebug // DefaultLogLevel 默认的日志级别 DefaultLogLevel = zapcore.DebugLevel - // DefaultLogSplitInterval 默认的日至切割时间 - DefaultLogSplitInterval = LogSplitIntervalHour + // DefaultLogSplitInterval 默认的日志切割时间 + DefaultLogSplitInterval = logger.TimeIntervalTypeHour ) // WSServerConfig WS-Server的配置 @@ -45,12 +46,13 @@ const ( // // Date : 7:02 下午 2021/4/17 type WSServerConfig struct { - Mode string // 运行模式 - LogEnable bool // 开启日志 - LogConsole bool // 开启控制台日志输出 - LogPath string // 日志路径 - LogLevel zapcore.Level // 日志等级 - LogSplitInterval string // 日至切割的时间间隔 + Mode string // 运行模式 + LogEnable bool // 开启日志 + LogConsole bool // 开启控制台日志输出 + LogPath string // 日志路径 + LogFile string // 日志文件名 + LogLevel zapcore.Level // 日志等级 + LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔 } // SetWSServerConfig 设置WS-Server的配置 @@ -65,14 +67,19 @@ type SetWSServerConfig func(wsc *WSServerConfig) // Author : go_developer@163.com<张德满> // // Date : 7:25 下午 2021/4/17 -func SetWSServerLogEnable(logPath string, logLevel zapcore.Level, splitInterval string) SetWSServerConfig { +func SetWSServerLogEnable(logPath string, logFile string, logLevel zapcore.Level, splitInterval logger.TimeIntervalType) SetWSServerConfig { return func(wsc *WSServerConfig) { - if splitInterval != LogSplitIntervalDay && splitInterval != LogSplitIntervalHour { + if splitInterval != logger.TimeIntervalTypeMinute && + splitInterval != logger.TimeIntervalTypeHour && + splitInterval != logger.TimeIntervalTypeDay && + splitInterval != logger.TimeIntervalTypeMonth && + splitInterval != logger.TimeIntervalTypeYear { // 传入非法值,默认按小时切割日志 - splitInterval = LogSplitIntervalHour + splitInterval = DefaultLogSplitInterval } wsc.LogEnable = true wsc.LogPath = logPath + wsc.LogFile = logFile wsc.LogLevel = logLevel wsc.LogSplitInterval = splitInterval } diff --git a/construct.go b/construct.go index 9a27273..a007d83 100644 --- a/construct.go +++ b/construct.go @@ -11,6 +11,10 @@ import ( "fmt" "sync" + "github.com/go-developer/gopkg/logger" + + "go.uber.org/zap" + "github.com/go-developer/websocket/message" "github.com/go-developer/websocket/config" @@ -36,8 +40,10 @@ import ( // // Date : 8:04 下午 2021/3/27 type Server struct { - ginRouter *gin.Engine - wsServer *melody.Melody + ginRouter *gin.Engine // GIN引擎 + wsServer *melody.Melody // websocket引擎 + conf *config.WSServerConfig // 配置 + loggerInstance *zap.Logger // 日志实例 } var ( @@ -69,10 +75,46 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { // 初始化ws server _ = lock.Lock() if _, exist := ginRouterTable[wsInstance.GetServerPort()]; !exist { - ginRouterTable[wsInstance.GetServerPort()] = &Server{ + wsSetConfigList := wsInstance.GetWSServerConfig() + if nil == wsSetConfigList { + wsSetConfigList = make([]config.SetWSServerConfig, 0) + } + s := &Server{ ginRouter: gin.Default(), wsServer: melody.New(), + conf: config.NewWSServerConfig(wsSetConfigList...), } + if s.conf.LogEnable { + // 开启了日志,初始化日志 + if len(s.conf.LogPath) == 0 { + panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,但是没有配置日志路径") + } + var ( + err error + logConf *logger.RotateLogConfig + ) + optionList := make([]logger.SetLoggerOptionFunc, 0) + if s.conf.LogConsole { + optionList = append(optionList, logger.WithConsoleOutput()) + } + if s.conf.LogConsole { + optionList = append(optionList, logger.WithConsoleOutput()) + } + if logConf, err = logger.NewRotateLogConfig(s.conf.LogPath, s.conf.LogFile, logger.WithTimeIntervalType(s.conf.LogSplitInterval)); nil != err { + panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,日志初始化失败, 失败原因 : " + err.Error()) + } + if s.loggerInstance, err = logger.NewLogger(s.conf.LogLevel, logConf, optionList...); nil != err { + panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,日志初始化失败, 失败原因 : " + err.Error()) + } + } + if nil == s.loggerInstance && s.conf.LogConsole { + var err error + // 没有配置文件日志, 但是配置了控制台输出 + if s.loggerInstance, err = logger.NewConsoleLogger(s.conf.LogLevel); nil != err { + panic(wsInstance.GetModuleFlag() + " 模块开启了控制台日志记录,日志初始化失败, 失败原因 : " + err.Error()) + } + } + ginRouterTable[wsInstance.GetServerPort()] = s } if _, exist := commandTable[wsInstance.GetModuleFlag()]; !exist { commandTable[wsInstance.GetModuleFlag()] = make(map[string]abstract.ICommand) @@ -133,6 +175,13 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { }) // 注册指令 for _, cmd := range wsInstance.GetCommandList() { + if nil != ginRouterTable[wsInstance.GetServerPort()].loggerInstance { + ginRouterTable[wsInstance.GetServerPort()].loggerInstance.Debug( + "长连接指令注册成功", + zap.String("module", wsInstance.GetModuleFlag()), + zap.String("command", cmd.GetCommand()), + ) + } commandTable[wsInstance.GetModuleFlag()][cmd.GetCommand()] = cmd } go func() { diff --git a/example/server.go b/example/server.go index 8704358..2972fc8 100644 --- a/example/server.go +++ b/example/server.go @@ -10,6 +10,9 @@ package main import ( "fmt" + "github.com/go-developer/gopkg/logger" + "go.uber.org/zap/zapcore" + "github.com/go-developer/websocket/config" "github.com/go-developer/websocket/message" @@ -59,6 +62,12 @@ func (e Example) GetServerPort() int { return 10099 } +func (e Example) GetWSServerConfig() []config.SetWSServerConfig { + return []config.SetWSServerConfig{ + config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, logger.TimeIntervalTypeHour), + } +} + type exampleCommand struct { } diff --git a/go.mod b/go.mod index c9a40ae..63a2ac1 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/gin-gonic/gin v1.6.3 - github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c + github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93 github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/golang/protobuf v1.5.1 // indirect github.com/gorilla/websocket v1.4.2 // indirect diff --git a/go.sum b/go.sum index 6cc799f..2eeaabc 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE= github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= +github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93 h1:05L+6oi0j9w5mR0/61DklCEwWe+7ock6rMncZ9XGvP0= +github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= @@ -62,7 +64,9 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= +github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4= github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= +github.com/lestrrat-go/strftime v1.0.4 h1:T1Rb9EPkAhgxKqbcMIPguPq8glqXTA1koF8n9BHElA8= github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=