1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00

gcp: Refactor to pass in custom writer (#52)

This enables the ability to output to custom writers (e.g. TeeWriters)
if necessary.
This commit is contained in:
Billy Lynch 2025-12-10 14:52:38 -05:00 committed by GitHub
parent 5a063eb822
commit 3bef2b2727
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@ package gcp
import (
"context"
"io"
"log/slog"
"os"
)
@ -15,8 +16,14 @@ type Handler struct {
handler slog.Handler
}
// NewHandler returns a new Handler that writes to stderr.
func NewHandler(level slog.Level) *Handler {
return &Handler{handler: slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
return NewHandlerForWriter(os.Stderr, level)
}
// NewHandlerForWriter returns a new Handler that writes to the given writer.
func NewHandlerForWriter(w io.Writer, level slog.Level) *Handler {
return &Handler{handler: slog.NewJSONHandler(w, &slog.HandlerOptions{
AddSource: true,
Level: level,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {