Skip to content

Commit

Permalink
fix: handle nil log valuers + remove stack traces from error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Apr 16, 2024
1 parent 3e6ea71 commit 10fcbf5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
36 changes: 36 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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))
}

Expand All @@ -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))
}

Expand All @@ -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),
Expand All @@ -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))
}

Expand Down Expand Up @@ -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))
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -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 != "" {
Expand Down
19 changes: 16 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 10fcbf5

Please sign in to comment.