Skip to content

Commit

Permalink
internal/logging: prototype slog logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Oct 30, 2024
1 parent 0960799 commit 394efb6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package logging

import (
"context"
"io"
"log"
"log/slog"
)

// Logger returns an opinionated logger for the given level.
func Logger(out io.Writer, level slog.Level) *slog.Logger {
return slog.New(textHandler(out, level))
}

func textHandler(out io.Writer, level slog.Level) slog.Handler {
return slog.NewTextHandler(out, &slog.HandlerOptions{
Level: level,
})
}

// TODO: need this?
func textLogger(out io.Writer) *log.Logger {
return log.New(out, "", 0)
}

// DiscardLogger returns a [slog.Logger] that discards all output.
func DiscardLogger() *slog.Logger {
return slog.New(DiscardHandler())
}

// DiscardHandler returns a [slog.Handler] that discards all output.
// It is an implementation of https://github.com/golang/go/issues/62005.
func DiscardHandler() slog.Handler {
return (*discardHandler)(nil)
}

type discardHandler struct {
slog.Handler
}

func (*discardHandler) Enabled(_ context.Context, _ slog.Level) bool {
return false
}

0 comments on commit 394efb6

Please sign in to comment.