Skip to content

Commit

Permalink
feat: add dump_packet option to log config
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 16, 2021
1 parent 7ad1a22 commit 5ee07a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cmd/gmqttd/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ plugin_order:
log:
level: info # debug | info | warn | error
format: text # json | text

# whether to dump MQTT packet in debug level
dump_packet: false



Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ var DefaultListeners = []*ListenerConfig{
// LogConfig is use to configure the log behaviors.
type LogConfig struct {
// Level is the log level. Possible values: debug, info, warn, error
Level string
Level string `yaml:"level"`
// Format is the log format. Possible values: json, text
Format string
Format string `yaml:"format"`
// DumpPacket indicates whether to dump MQTT packet in debug level.
DumpPacket bool `yaml:"dump_packet"`
}

func (l LogConfig) Validate() error {
Expand Down
28 changes: 16 additions & 12 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@ func (client *client) writeLoop() {
}

func (client *client) writePacket(packet packets.Packet) error {
if ce := zaplog.Check(zapcore.DebugLevel, "sending packet"); ce != nil {
ce.Write(
zap.String("packet", packet.String()),
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
zap.String("client_id", client.opts.ClientID),
)
if client.server.config.Log.DumpPacket {
if ce := zaplog.Check(zapcore.DebugLevel, "sending packet"); ce != nil {
ce.Write(
zap.String("packet", packet.String()),
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
zap.String("client_id", client.opts.ClientID),
)
}
}
err := client.packetWriter.WritePacket(packet)
if err != nil {
Expand Down Expand Up @@ -388,12 +390,14 @@ func (client *client) readLoop() {
client.in <- packet
<-client.connected
srv.statsManager.packetReceived(packet, client.opts.ClientID)
if ce := zaplog.Check(zapcore.DebugLevel, "received packet"); ce != nil {
ce.Write(
zap.String("packet", packet.String()),
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
zap.String("client_id", client.opts.ClientID),
)
if client.server.config.Log.DumpPacket {
if ce := zaplog.Check(zapcore.DebugLevel, "received packet"); ce != nil {
ce.Write(
zap.String("packet", packet.String()),
zap.String("remote_addr", client.rwc.RemoteAddr().String()),
zap.String("client_id", client.opts.ClientID),
)
}
}
}
}
Expand Down

0 comments on commit 5ee07a7

Please sign in to comment.