diff --git a/examples/zinx_logger/server/my_logger.go b/examples/zinx_logger/server/my_logger.go index d1525771..6fc28046 100644 --- a/examples/zinx_logger/server/my_logger.go +++ b/examples/zinx_logger/server/my_logger.go @@ -41,3 +41,7 @@ func (l *MyLogger) DebugFX(ctx context.Context, format string, v ...interface{}) fmt.Println(ctx) fmt.Printf(format, v...) } + +func (l *MyLogger) DebugEnabled() bool { + return true +} diff --git a/ziface/ilogger.go b/ziface/ilogger.go index 20f57566..2e7b7fee 100644 --- a/ziface/ilogger.go +++ b/ziface/ilogger.go @@ -12,4 +12,7 @@ type ILogger interface { InfoFX(ctx context.Context, format string, v ...interface{}) ErrorFX(ctx context.Context, format string, v ...interface{}) DebugFX(ctx context.Context, format string, v ...interface{}) + + // 此处增加了 interface 定义,可能会导致原有第三方实现无法通过编译 + DebugEnabled() bool } diff --git a/zlog/default.go b/zlog/default.go index d0637dce..4239374c 100644 --- a/zlog/default.go +++ b/zlog/default.go @@ -38,6 +38,10 @@ func (log *zinxDefaultLog) DebugFX(ctx context.Context, format string, v ...inte StdZinxLog.Debugf(format, v...) } +func (log *zinxDefaultLog) DebugEnabled() bool { + return StdZinxLog.DebugEnabled() +} + func SetLogger(newlog ziface.ILogger) { zLogInstance = newlog } diff --git a/zlog/logger_core.go b/zlog/logger_core.go index 143e64c8..3a57b38d 100644 --- a/zlog/logger_core.go +++ b/zlog/logger_core.go @@ -426,6 +426,10 @@ func (log *ZinxLoggerCore) SetLogLevel(logLevel int) { log.isolationLevel = logLevel } +func (log *ZinxLoggerCore) DebugEnabled() bool { + return log.isolationLevel == LogDebug +} + // Convert an integer to a fixed-length string, where the width of the string should be greater than 0 // Ensure that the buffer has sufficient capacity // (将一个整形转换成一个固定长度的字符串,字符串宽度应该是大于0的 diff --git a/zlog/stdzlog.go b/zlog/stdzlog.go index df74ee2d..0d0083ab 100644 --- a/zlog/stdzlog.go +++ b/zlog/stdzlog.go @@ -121,6 +121,10 @@ func Stack(v ...interface{}) { StdZinxLog.Stack(v...) } +func DebugEnabled() bool { + return StdZinxLog.DebugEnabled() +} + func init() { // Since the StdZinxLog object wraps all output methods with an extra layer, the call depth is one more than a normal logger object // The call depth of a regular zinxLogger object is 2, and the call depth of StdZinxLog is 3 diff --git a/znet/connection.go b/znet/connection.go index 4973bea1..58968558 100644 --- a/znet/connection.go +++ b/znet/connection.go @@ -251,7 +251,9 @@ func (c *Connection) StartReader() { zlog.Ins().ErrorF("read msg head [read datalen=%d], error = %s", n, err) return } - zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + if zlog.Ins().DebugEnabled() { + zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + } // If normal data is read from the peer, update the heartbeat detection Active state // (正常读取到对端数据,更新心跳检测Active状态) @@ -269,7 +271,9 @@ func (c *Connection) StartReader() { continue } for _, bytes := range bufArrays { - // zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes)) + //if zlog.Ins().DebugEnabled() { + // zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + //} msg := zpack.NewMessage(uint32(len(bytes)), bytes) // Get the current client's Request data // (得到当前客户端请求的Request数据) diff --git a/znet/kcp_connection.go b/znet/kcp_connection.go index e42183ec..6a33742b 100644 --- a/znet/kcp_connection.go +++ b/znet/kcp_connection.go @@ -236,7 +236,9 @@ func (c *KcpConnection) StartReader() { zlog.Ins().ErrorF("read msg head [read datalen=%d], error = %s", n, err) return } - zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + if zlog.Ins().DebugEnabled() { + zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + } // If normal data is read from the peer, update the heartbeat detection Active state // (正常读取到对端数据,更新心跳检测Active状态) @@ -254,7 +256,9 @@ func (c *KcpConnection) StartReader() { continue } for _, bytes := range bufArrays { - // zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes)) + //if zlog.Ins().DebugEnabled() { + // zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + //} msg := zpack.NewMessage(uint32(len(bytes)), bytes) // Get the current client's Request data // (得到当前客户端请求的Request数据) diff --git a/znet/msghandler.go b/znet/msghandler.go index 9d11558a..d8a2dcdb 100644 --- a/znet/msghandler.go +++ b/znet/msghandler.go @@ -301,7 +301,9 @@ func (mh *MsgHandle) SendMsgToTaskQueue(request ziface.IRequest) { // zlog.Ins().DebugF("Add ConnID=%d request msgID=%d to workerID=%d", request.GetConnection().GetConnID(), request.GetMsgID(), workerID) // Send the request message to the task queue mh.TaskQueue[workerID] <- request - zlog.Ins().DebugF("SendMsgToTaskQueue-->%s", hex.EncodeToString(request.GetData())) + if zlog.Ins().DebugEnabled() { + zlog.Ins().DebugF("SendMsgToTaskQueue-->%s", hex.EncodeToString(request.GetData())) + } } // doFuncHandler handles functional requests (执行函数式请求) diff --git a/znet/ws_connection.go b/znet/ws_connection.go index 12babfbb..851e5ee4 100644 --- a/znet/ws_connection.go +++ b/znet/ws_connection.go @@ -235,7 +235,9 @@ func (c *WsConnection) StartReader() { zlog.Ins().ErrorF("read msg head [read datalen=%d], error = %s", n, err.Error()) return } - zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + if zlog.Ins().DebugEnabled() { + zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(buffer[0:n])) + } // Update the Active status of heartbeat detection normally after reading data from the peer. // (正常读取到对端数据,更新心跳检测Active状态) @@ -253,7 +255,9 @@ func (c *WsConnection) StartReader() { continue } for _, bytes := range bufArrays { - zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes)) + if zlog.Ins().DebugEnabled() { + zlog.Ins().DebugF("read buffer %s \n", hex.EncodeToString(bytes)) + } msg := zpack.NewMessage(uint32(len(bytes)), bytes) // Get the Request data requested by the current client. // (得到当前客户端请求的Request数据)