-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While less performant, its in the stdlib which is probably more suitable for an SDK. Users can register a slog handler to intercept logs
- Loading branch information
1 parent
df3a918
commit bb29ac2
Showing
10 changed files
with
337 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"reflect" | ||
|
||
"github.com/restatedev/sdk-go/rcontext" | ||
) | ||
|
||
const ( | ||
LevelTrace slog.Level = -8 | ||
) | ||
|
||
type typeValue struct{ inner any } | ||
|
||
func (t typeValue) LogValue() slog.Value { | ||
return slog.StringValue(reflect.TypeOf(t.inner).String()) | ||
} | ||
|
||
func Type(key string, value any) slog.Attr { | ||
return slog.Any(key, typeValue{value}) | ||
} | ||
|
||
type stringerValue[T fmt.Stringer] struct{ inner T } | ||
|
||
func (t stringerValue[T]) LogValue() slog.Value { | ||
return slog.StringValue(t.inner.String()) | ||
} | ||
|
||
func Stringer[T fmt.Stringer](key string, value T) slog.Attr { | ||
return slog.Any(key, slog.AnyValue(stringerValue[T]{value})) | ||
} | ||
|
||
func Error(err error) slog.Attr { | ||
return slog.String("err", err.Error()) | ||
} | ||
|
||
type Logger struct { | ||
slog *slog.Logger | ||
} | ||
|
||
func New(source rcontext.LogSource, isReplaying func() bool, h slog.Handler) *Logger { | ||
return &Logger{slog.New(&contextInjectingHandler{source, isReplaying, h})} | ||
} | ||
|
||
func (l *Logger) Inner() *slog.Logger { | ||
return l.slog | ||
} | ||
|
||
func (l *Logger) With(args ...any) *Logger { | ||
return &Logger{slog: l.slog.With(args...)} | ||
} | ||
|
||
func (l *Logger) Error(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
l.slog.LogAttrs(ctx, slog.LevelError, msg, attrs...) | ||
} | ||
|
||
func (l *Logger) Warn(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
l.slog.LogAttrs(ctx, slog.LevelWarn, msg, attrs...) | ||
} | ||
|
||
func (l *Logger) Info(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
l.slog.LogAttrs(ctx, slog.LevelInfo, msg, attrs...) | ||
} | ||
|
||
func (l *Logger) Debug(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
l.slog.LogAttrs(ctx, slog.LevelDebug, msg, attrs...) | ||
} | ||
|
||
func (l *Logger) Trace(ctx context.Context, msg string, attrs ...slog.Attr) { | ||
l.slog.LogAttrs(ctx, LevelTrace, msg, attrs...) | ||
} | ||
|
||
type contextInjectingHandler struct { | ||
source rcontext.LogSource | ||
isReplaying func() bool | ||
inner slog.Handler | ||
} | ||
|
||
func (d *contextInjectingHandler) Enabled(ctx context.Context, l slog.Level) bool { | ||
return d.inner.Enabled(rcontext.WithLogContext(ctx, d.source, d.isReplaying), l) | ||
} | ||
|
||
func (d *contextInjectingHandler) Handle(ctx context.Context, record slog.Record) error { | ||
return d.inner.Handle(rcontext.WithLogContext(ctx, d.source, d.isReplaying), record) | ||
} | ||
|
||
func (d *contextInjectingHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return &contextInjectingHandler{d.source, d.isReplaying, d.inner.WithAttrs(attrs)} | ||
} | ||
|
||
func (d *contextInjectingHandler) WithGroup(name string) slog.Handler { | ||
return &contextInjectingHandler{d.source, d.isReplaying, d.inner.WithGroup(name)} | ||
} | ||
|
||
var _ slog.Handler = &contextInjectingHandler{} | ||
|
||
type dropReplayHandler struct { | ||
isReplaying func() bool | ||
inner slog.Handler | ||
} | ||
|
||
func NewDropReplayHandler(isReplaying func() bool, handler slog.Handler) slog.Handler { | ||
return &dropReplayHandler{isReplaying, handler} | ||
} | ||
|
||
func (d *dropReplayHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
if d.isReplaying() { | ||
return false | ||
} | ||
return d.inner.Enabled(ctx, level) | ||
} | ||
|
||
func (d *dropReplayHandler) Handle(ctx context.Context, record slog.Record) error { | ||
return d.inner.Handle(ctx, record) | ||
} | ||
|
||
func (d *dropReplayHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return &dropReplayHandler{d.isReplaying, d.inner.WithAttrs(attrs)} | ||
} | ||
|
||
func (d *dropReplayHandler) WithGroup(name string) slog.Handler { | ||
return &dropReplayHandler{d.isReplaying, d.inner.WithGroup(name)} | ||
} | ||
|
||
var _ slog.Handler = &dropReplayHandler{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.