Skip to content

Commit aab9422

Browse files
committed
interface+log: add new structured log methods
And let subLog implement the methods by calling its existing methods. Note that subLog will not produce structured logs as of this commit. In future, it can be updated to use an slog.Logger and this package can implement a default slog.Handler.
1 parent 668b671 commit aab9422

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

interface.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package btclog
66

7+
import "context"
8+
79
// Logger is an interface which describes a level-based logger. A default
810
// implementation of Logger is implemented by this package and can be created
911
// by calling (*Backend).Logger.
@@ -56,6 +58,30 @@ type Logger interface {
5658
// and writes to log with LevelCritical.
5759
Critical(v ...any)
5860

61+
// TraceS writes a structured log with the given message and key-value
62+
// pair attributes with LevelTrace to the log.
63+
TraceS(ctx context.Context, msg string, attrs ...any)
64+
65+
// DebugS writes a structured log with the given message and key-value
66+
// pair attributes with LevelDebug to the log.
67+
DebugS(ctx context.Context, msg string, attrs ...any)
68+
69+
// InfoS writes a structured log with the given message and key-value
70+
// pair attributes with LevelInfo to the log.
71+
InfoS(ctx context.Context, msg string, attrs ...any)
72+
73+
// WarnS writes a structured log with the given message and key-value
74+
// pair attributes with LevelWarn to the log.
75+
WarnS(ctx context.Context, msg string, err error, attrs ...any)
76+
77+
// ErrorS writes a structured log with the given message and key-value
78+
// pair attributes with LevelError to the log.
79+
ErrorS(ctx context.Context, msg string, err error, attrs ...any)
80+
81+
// CriticalS writes a structured log with the given message and
82+
// key-value pair attributes with LevelCritical to the log.
83+
CriticalS(ctx context.Context, msg string, err error, attrs ...any)
84+
5985
// Level returns the current logging level.
6086
Level() Level
6187

log.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ package btclog
3434

3535
import (
3636
"bytes"
37+
"context"
3738
"fmt"
3839
"io"
40+
"log/slog"
3941
"os"
4042
"runtime"
4143
"strings"
@@ -417,6 +419,63 @@ func (l *subLog) Criticalf(format string, args ...any) {
417419
}
418420
}
419421

422+
// TraceS writes a structured log with the given message and key-value pair
423+
// attributes with LevelTrace to the log.
424+
//
425+
// This is part of the Logger interface implementation.
426+
func (l *subLog) TraceS(_ context.Context, msg string, attrs ...any) {
427+
l.Tracef(msg, attrs...)
428+
}
429+
430+
// DebugS writes a structured log with the given message and key-value pair
431+
// attributes with LevelDebug to the log.
432+
//
433+
// This is part of the Logger interface implementation.
434+
func (l *subLog) DebugS(_ context.Context, msg string, attrs ...any) {
435+
l.Debugf(msg, attrs...)
436+
}
437+
438+
// InfoS writes a structured log with the given message and key-value pair
439+
// attributes with LevelInfo to the log.
440+
//
441+
// This is part of the Logger interface implementation.
442+
func (l *subLog) InfoS(_ context.Context, msg string, attrs ...any) {
443+
l.Infof(msg, attrs...)
444+
}
445+
446+
// WarnS writes a structured log with the given message and key-value pair
447+
// attributes with LevelWarn to the log.
448+
//
449+
// This is part of the Logger interface implementation.
450+
func (l *subLog) WarnS(_ context.Context, msg string, err error, attrs ...any) {
451+
if err != nil {
452+
attrs = append([]any{slog.String("err", err.Error())}, attrs...)
453+
}
454+
l.Warnf(msg, attrs...)
455+
}
456+
457+
// ErrorS writes a structured log with the given message and key-value pair
458+
// attributes with LevelError to the log.
459+
//
460+
// This is part of the Logger interface implementation.
461+
func (l *subLog) ErrorS(_ context.Context, msg string, err error, attrs ...any) {
462+
if err != nil {
463+
attrs = append([]any{slog.String("err", err.Error())}, attrs...)
464+
}
465+
l.Errorf(msg, attrs...)
466+
}
467+
468+
// CriticalS writes a structured log with the given message and key-value pair
469+
// attributes with LevelCritical to the log.
470+
//
471+
// This is part of the Logger interface implementation.
472+
func (l *subLog) CriticalS(_ context.Context, msg string, err error, attrs ...any) {
473+
if err != nil {
474+
attrs = append([]any{slog.String("err", err.Error())}, attrs...)
475+
}
476+
l.Criticalf(msg, attrs...)
477+
}
478+
420479
// Level returns the current logging level
421480
//
422481
// This is part of the Logger interface implementation.

0 commit comments

Comments
 (0)