-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/logging: prototype slog logging
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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
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 | ||
} |