-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add slog handler #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,189 @@ | ||
| package logger | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| // Handler is a slog.Handler backed by hamba/logger. It does not synchronize writes; | ||
| // wrap the writer with NewSyncWriter for concurrent use in non-posix environments. | ||
| type Handler struct { | ||
| w io.Writer | ||
| isDiscard bool | ||
| fmtr Formatter | ||
| lvl slog.Level | ||
|
nrwiersma marked this conversation as resolved.
|
||
|
|
||
| ctx []byte | ||
| prefix []byte | ||
|
|
||
| openGroups int | ||
| } | ||
|
nrwiersma marked this conversation as resolved.
|
||
|
|
||
| // NewHandler returns a new Handler. | ||
| func NewHandler(w io.Writer, fmtr Formatter, lvl slog.Level) *Handler { | ||
| return &Handler{ | ||
| w: w, | ||
| isDiscard: w == io.Discard, | ||
| fmtr: fmtr, | ||
| lvl: lvl, | ||
| } | ||
| } | ||
|
|
||
| // Enabled returns false when lvl is below the configured minimum or the | ||
| // underlying writer is io.Discard. | ||
| func (h *Handler) Enabled(_ context.Context, lvl slog.Level) bool { | ||
| return !h.isDiscard && lvl >= h.lvl | ||
| } | ||
|
|
||
| // Handle writes the record to the underlying writer. | ||
| func (h *Handler) Handle(_ context.Context, r slog.Record) error { | ||
| e := newEvent(h.fmtr) | ||
| e.prefix = append(e.prefix, h.prefix...) | ||
|
|
||
| e.fmtr.AppendBeginMarker(e.buf) | ||
| e.fmtr.WriteMessage(e.buf, r.Time, mapSlogLevel(r.Level), r.Message) | ||
| e.buf.Write(h.ctx) | ||
|
|
||
| r.Attrs(func(a slog.Attr) bool { | ||
| appendAttr(e, a) | ||
| return true | ||
| }) | ||
|
|
||
| for range h.openGroups { | ||
| e.prefix = e.fmtr.AppendGroupEnd(e.buf, e.prefix) | ||
| } | ||
|
|
||
| e.fmtr.AppendEndMarker(e.buf) | ||
| e.fmtr.AppendLineBreak(e.buf) | ||
|
|
||
| _, err := h.w.Write(e.buf.Bytes()) | ||
| putEvent(e) | ||
| return err | ||
| } | ||
|
|
||
| // WithAttrs returns a new Handler with attrs pre-serialised into the context. | ||
| func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
| if len(attrs) == 0 { | ||
| return h | ||
| } | ||
|
|
||
| e := newEvent(h.fmtr) | ||
| e.buf.Write(h.ctx) | ||
| e.prefix = append(e.prefix, h.prefix...) | ||
|
|
||
| for _, a := range attrs { | ||
| appendAttr(e, a) | ||
| } | ||
|
|
||
| newCtx := make([]byte, e.buf.Len()) | ||
| copy(newCtx, e.buf.Bytes()) | ||
|
|
||
| putEvent(e) | ||
|
|
||
| return &Handler{ | ||
| fmtr: h.fmtr, | ||
| w: h.w, | ||
| isDiscard: h.isDiscard, | ||
| lvl: h.lvl, | ||
| ctx: newCtx, | ||
| prefix: h.prefix, | ||
| openGroups: h.openGroups, | ||
| } | ||
| } | ||
|
|
||
| // WithGroup returns a new Handler with name appended to the group stack. | ||
| // An empty name is a no-op per the slog spec. | ||
| func (h *Handler) WithGroup(name string) slog.Handler { | ||
| if name == "" { | ||
| return h | ||
| } | ||
|
|
||
| e := newEvent(h.fmtr) | ||
| e.buf.Write(h.ctx) | ||
| e.prefix = append(e.prefix, h.prefix...) | ||
|
|
||
| e.prefix = h.fmtr.AppendGroupStart(e.buf, e.prefix, name) | ||
|
|
||
| newCtx := make([]byte, e.buf.Len()) | ||
| copy(newCtx, e.buf.Bytes()) | ||
| newPrefix := make([]byte, len(e.prefix)) | ||
| copy(newPrefix, e.prefix) | ||
|
|
||
| putEvent(e) | ||
|
|
||
| return &Handler{ | ||
| fmtr: h.fmtr, | ||
| w: h.w, | ||
| isDiscard: h.isDiscard, | ||
| lvl: h.lvl, | ||
| ctx: newCtx, | ||
| prefix: newPrefix, | ||
| openGroups: h.openGroups + 1, | ||
| } | ||
| } | ||
|
|
||
| func appendAttr(e *Event, a slog.Attr) { | ||
| a.Value = a.Value.Resolve() | ||
| if a.Equal(slog.Attr{}) { | ||
| return | ||
| } | ||
|
|
||
| switch a.Value.Kind() { | ||
| case slog.KindGroup: | ||
| appendGroup(e, a.Key, a.Value) | ||
| case slog.KindString: | ||
| e.AppendString(a.Key, a.Value.String()) | ||
| case slog.KindInt64: | ||
| e.AppendInt(a.Key, a.Value.Int64()) | ||
| case slog.KindUint64: | ||
| e.AppendUint(a.Key, a.Value.Uint64()) | ||
| case slog.KindFloat64: | ||
| e.AppendFloat(a.Key, a.Value.Float64()) | ||
| case slog.KindBool: | ||
| e.AppendBool(a.Key, a.Value.Bool()) | ||
| case slog.KindTime: | ||
| e.AppendTime(a.Key, a.Value.Time()) | ||
| case slog.KindDuration: | ||
| e.AppendDuration(a.Key, a.Value.Duration()) | ||
| default: | ||
| e.AppendInterface(a.Key, a.Value.Any()) | ||
| } | ||
| } | ||
|
|
||
| func appendGroup(e *Event, name string, val slog.Value) { | ||
| subs := val.Group() | ||
| if len(subs) == 0 { | ||
| return | ||
| } | ||
|
|
||
| if name == "" { | ||
| // Per the slog spec, an anonymous group is flattened into the | ||
| // enclosing scope. | ||
| for _, a := range subs { | ||
| appendAttr(e, a) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| e.OpenGroup(name) | ||
| for _, a := range subs { | ||
| appendAttr(e, a) | ||
| } | ||
| e.CloseGroup() | ||
| } | ||
|
|
||
| // mapSlogLevel maps to logger.Level. Custom levels below Debug clamp to | ||
| // Debug; above Error clamp to Error. Trace and Crit are never produced. | ||
| func mapSlogLevel(lvl slog.Level) Level { | ||
| switch { | ||
| case lvl >= slog.LevelError: | ||
| return Error | ||
| case lvl >= slog.LevelWarn: | ||
| return Warn | ||
| case lvl >= slog.LevelInfo: | ||
| return Info | ||
| default: | ||
| return Debug | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.