diff --git a/common/common.go b/common/common.go index a755195c..671bbd98 100644 --- a/common/common.go +++ b/common/common.go @@ -190,6 +190,10 @@ type ConnectResult struct { } func (c *ConnectResult) LogValue() slog.Value { + if c == nil { + return slog.StringValue("nil") + } + return slog.GroupValue( slog.String("status", StatusName(c.Status)), slog.Any("transmissions", logger.CompactValues(c.Transmissions)), @@ -231,6 +235,10 @@ type CommandResult struct { } func (c *CommandResult) LogValue() slog.Value { + if c == nil { + return slog.StringValue("nil") + } + return slog.GroupValue( slog.String("status", StatusName(c.Status)), slog.Any("streams", logger.CompactValues(c.Streams)), @@ -263,6 +271,10 @@ type HistoryPosition struct { } func (hp *HistoryPosition) LogValue() slog.Value { + if hp == nil { + return slog.StringValue("nil") + } + return slog.GroupValue(slog.String("epoch", hp.Epoch), slog.Uint64("offset", hp.Offset)) } @@ -276,6 +288,10 @@ type HistoryRequest struct { } func (hr *HistoryRequest) LogValue() slog.Value { + if hr == nil { + return slog.StringValue("nil") + } + return slog.GroupValue(slog.Int64("since", hr.Since), slog.Any("streams", hr.Streams)) } @@ -288,6 +304,10 @@ type Message struct { } func (m *Message) LogValue() slog.Value { + if m == nil { + return slog.StringValue("nil") + } + return slog.GroupValue( slog.String("command", m.Command), slog.String("identifier", m.Identifier), @@ -307,6 +327,10 @@ type StreamMessageMetadata struct { } func (smm *StreamMessageMetadata) LogValue() slog.Value { + if smm == nil { + return slog.StringValue("nil") + } + return slog.GroupValue(slog.String("exclude_socket", smm.ExcludeSocket)) } @@ -374,6 +398,10 @@ type RemoteCommandMessage struct { } func (m *RemoteCommandMessage) LogValue() slog.Value { + if m == nil { + return slog.StringValue("nil") + } + return slog.GroupValue(slog.String("command", m.Command), slog.Any("payload", m.Payload)) } @@ -394,6 +422,10 @@ type RemoteDisconnectMessage struct { } func (m *RemoteDisconnectMessage) LogValue() slog.Value { + if m == nil { + return slog.StringValue("nil") + } + return slog.GroupValue(slog.String("ids", m.Identifier), slog.Bool("reconnect", m.Reconnect)) } @@ -446,6 +478,10 @@ type Reply struct { } func (r *Reply) LogValue() slog.Value { + if r == nil { + return slog.StringValue("nil") + } + attrs := []slog.Attr{} if r.Type != "" { diff --git a/logger/logger.go b/logger/logger.go index 24d85779..323f318c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -22,9 +22,10 @@ func InitLogger(format string, level string) (slog.Handler, error) { case "text": { opts := &tint.Options{ - Level: logLevel, - NoColor: !utils.IsTTY(), - TimeFormat: "2006-01-02 15:04:05.000", + Level: logLevel, + NoColor: !utils.IsTTY(), + TimeFormat: "2006-01-02 15:04:05.000", + ReplaceAttr: transformAttr, } handler = tint.NewHandler(os.Stdout, opts) } @@ -59,3 +60,15 @@ func parseLevel(level string) (slog.Level, error) { return lvl, nil } + +// Perform some transformations before sending the log record to the handler: +// - Transform errors into messages to avoid showing stack traces +func transformAttr(groups []string, attr slog.Attr) slog.Attr { + if attr.Key == "err" || attr.Key == "error" { + if err, ok := attr.Value.Any().(error); ok { + return slog.Attr{Key: attr.Key, Value: slog.StringValue(err.Error())} + } + } + + return attr +}