Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 10fcbf5

Browse files
committed
fix: handle nil log valuers + remove stack traces from error logs
1 parent 3e6ea71 commit 10fcbf5

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

common/common.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ type ConnectResult struct {
190190
}
191191

192192
func (c *ConnectResult) LogValue() slog.Value {
193+
if c == nil {
194+
return slog.StringValue("nil")
195+
}
196+
193197
return slog.GroupValue(
194198
slog.String("status", StatusName(c.Status)),
195199
slog.Any("transmissions", logger.CompactValues(c.Transmissions)),
@@ -231,6 +235,10 @@ type CommandResult struct {
231235
}
232236

233237
func (c *CommandResult) LogValue() slog.Value {
238+
if c == nil {
239+
return slog.StringValue("nil")
240+
}
241+
234242
return slog.GroupValue(
235243
slog.String("status", StatusName(c.Status)),
236244
slog.Any("streams", logger.CompactValues(c.Streams)),
@@ -263,6 +271,10 @@ type HistoryPosition struct {
263271
}
264272

265273
func (hp *HistoryPosition) LogValue() slog.Value {
274+
if hp == nil {
275+
return slog.StringValue("nil")
276+
}
277+
266278
return slog.GroupValue(slog.String("epoch", hp.Epoch), slog.Uint64("offset", hp.Offset))
267279
}
268280

@@ -276,6 +288,10 @@ type HistoryRequest struct {
276288
}
277289

278290
func (hr *HistoryRequest) LogValue() slog.Value {
291+
if hr == nil {
292+
return slog.StringValue("nil")
293+
}
294+
279295
return slog.GroupValue(slog.Int64("since", hr.Since), slog.Any("streams", hr.Streams))
280296
}
281297

@@ -288,6 +304,10 @@ type Message struct {
288304
}
289305

290306
func (m *Message) LogValue() slog.Value {
307+
if m == nil {
308+
return slog.StringValue("nil")
309+
}
310+
291311
return slog.GroupValue(
292312
slog.String("command", m.Command),
293313
slog.String("identifier", m.Identifier),
@@ -307,6 +327,10 @@ type StreamMessageMetadata struct {
307327
}
308328

309329
func (smm *StreamMessageMetadata) LogValue() slog.Value {
330+
if smm == nil {
331+
return slog.StringValue("nil")
332+
}
333+
310334
return slog.GroupValue(slog.String("exclude_socket", smm.ExcludeSocket))
311335
}
312336

@@ -374,6 +398,10 @@ type RemoteCommandMessage struct {
374398
}
375399

376400
func (m *RemoteCommandMessage) LogValue() slog.Value {
401+
if m == nil {
402+
return slog.StringValue("nil")
403+
}
404+
377405
return slog.GroupValue(slog.String("command", m.Command), slog.Any("payload", m.Payload))
378406
}
379407

@@ -394,6 +422,10 @@ type RemoteDisconnectMessage struct {
394422
}
395423

396424
func (m *RemoteDisconnectMessage) LogValue() slog.Value {
425+
if m == nil {
426+
return slog.StringValue("nil")
427+
}
428+
397429
return slog.GroupValue(slog.String("ids", m.Identifier), slog.Bool("reconnect", m.Reconnect))
398430
}
399431

@@ -446,6 +478,10 @@ type Reply struct {
446478
}
447479

448480
func (r *Reply) LogValue() slog.Value {
481+
if r == nil {
482+
return slog.StringValue("nil")
483+
}
484+
449485
attrs := []slog.Attr{}
450486

451487
if r.Type != "" {

logger/logger.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ func InitLogger(format string, level string) (slog.Handler, error) {
2222
case "text":
2323
{
2424
opts := &tint.Options{
25-
Level: logLevel,
26-
NoColor: !utils.IsTTY(),
27-
TimeFormat: "2006-01-02 15:04:05.000",
25+
Level: logLevel,
26+
NoColor: !utils.IsTTY(),
27+
TimeFormat: "2006-01-02 15:04:05.000",
28+
ReplaceAttr: transformAttr,
2829
}
2930
handler = tint.NewHandler(os.Stdout, opts)
3031
}
@@ -59,3 +60,15 @@ func parseLevel(level string) (slog.Level, error) {
5960

6061
return lvl, nil
6162
}
63+
64+
// Perform some transformations before sending the log record to the handler:
65+
// - Transform errors into messages to avoid showing stack traces
66+
func transformAttr(groups []string, attr slog.Attr) slog.Attr {
67+
if attr.Key == "err" || attr.Key == "error" {
68+
if err, ok := attr.Value.Any().(error); ok {
69+
return slog.Attr{Key: attr.Key, Value: slog.StringValue(err.Error())}
70+
}
71+
}
72+
73+
return attr
74+
}

0 commit comments

Comments
 (0)