Skip to content

Commit 5ee07a7

Browse files
author
zhanglifang@chinatelecom.cn
committed
feat: add dump_packet option to log config
1 parent 7ad1a22 commit 5ee07a7

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

cmd/gmqttd/default_config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ plugin_order:
7878
log:
7979
level: info # debug | info | warn | error
8080
format: text # json | text
81-
81+
# whether to dump MQTT packet in debug level
82+
dump_packet: false
8283

8384

8485

config/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ var DefaultListeners = []*ListenerConfig{
7272
// LogConfig is use to configure the log behaviors.
7373
type LogConfig struct {
7474
// Level is the log level. Possible values: debug, info, warn, error
75-
Level string
75+
Level string `yaml:"level"`
7676
// Format is the log format. Possible values: json, text
77-
Format string
77+
Format string `yaml:"format"`
78+
// DumpPacket indicates whether to dump MQTT packet in debug level.
79+
DumpPacket bool `yaml:"dump_packet"`
7880
}
7981

8082
func (l LogConfig) Validate() error {

server/client.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,14 @@ func (client *client) writeLoop() {
319319
}
320320

321321
func (client *client) writePacket(packet packets.Packet) error {
322-
if ce := zaplog.Check(zapcore.DebugLevel, "sending packet"); ce != nil {
323-
ce.Write(
324-
zap.String("packet", packet.String()),
325-
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
326-
zap.String("client_id", client.opts.ClientID),
327-
)
322+
if client.server.config.Log.DumpPacket {
323+
if ce := zaplog.Check(zapcore.DebugLevel, "sending packet"); ce != nil {
324+
ce.Write(
325+
zap.String("packet", packet.String()),
326+
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
327+
zap.String("client_id", client.opts.ClientID),
328+
)
329+
}
328330
}
329331
err := client.packetWriter.WritePacket(packet)
330332
if err != nil {
@@ -388,12 +390,14 @@ func (client *client) readLoop() {
388390
client.in <- packet
389391
<-client.connected
390392
srv.statsManager.packetReceived(packet, client.opts.ClientID)
391-
if ce := zaplog.Check(zapcore.DebugLevel, "received packet"); ce != nil {
392-
ce.Write(
393-
zap.String("packet", packet.String()),
394-
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
395-
zap.String("client_id", client.opts.ClientID),
396-
)
393+
if client.server.config.Log.DumpPacket {
394+
if ce := zaplog.Check(zapcore.DebugLevel, "received packet"); ce != nil {
395+
ce.Write(
396+
zap.String("packet", packet.String()),
397+
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
398+
zap.String("client_id", client.opts.ClientID),
399+
)
400+
}
397401
}
398402
}
399403
}

0 commit comments

Comments
 (0)