Skip to content

Commit

Permalink
docs: add godocs
Browse files Browse the repository at this point in the history
Signed-off-by: TJ Hoplock <[email protected]>
  • Loading branch information
tjhop committed Nov 26, 2024
1 parent 705216f commit 63e5ba0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func NewGoKitHandler(logger log.Logger, level slog.Leveler) slog.Handler {
return &GoKitHandler{logger: logger, level: level}
}

// Enabled returns true if the internal slog.Leveler is enabled for the
// provided log level. It implements slog.Handler.
func (h *GoKitHandler) Enabled(_ context.Context, level slog.Level) bool {
if h.level == nil {
h.level = &slog.LevelVar{} // Info level by default.
Expand All @@ -49,6 +51,10 @@ func (h *GoKitHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.level.Level()
}

// Handler take an slog.Record created by an slog.Logger and dispatches the log
// call to the internal go-kit logger. Groups and attributes created by slog
// are formatted and added to the log call as individual key/value pairs. It
// implements slog.Handler.
func (h *GoKitHandler) Handle(_ context.Context, record slog.Record) error {
if h.logger == nil {
h.logger = defaultGoKitLogger
Expand Down Expand Up @@ -77,6 +83,8 @@ func (h *GoKitHandler) Handle(_ context.Context, record slog.Record) error {
return logger.Log(pairs...)
}

// WithAttrs formats the provided attributes and caches them in the handler to
// attach to all future log calls. It implements slog.Handler.
func (h *GoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
pairs := make([]any, 0, 2*len(attrs))
for _, a := range attrs {
Expand All @@ -95,6 +103,8 @@ func (h *GoKitHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
}
}

// WithGroup sets the group prefix string and caches it within the handler to
// use to prefix all future log attribute pairs. It implements slog.Handler.
func (h *GoKitHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
Expand Down

0 comments on commit 63e5ba0

Please sign in to comment.