Skip to content

Commit

Permalink
support configurable log levels
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh committed Feb 2, 2024
1 parent 4316066 commit f42cf6d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions gcp/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import (
"github.com/chainguard-dev/clog/gcp"
)

// Set up structured logging
func init() { slog.SetDefault(slog.New(gcp.NewHandler())) }
// Set up structured logging at Info+ level.
// TODO: Make the level configurable by env var or flag; or just remove the init package.
func init() { slog.SetDefault(slog.New(gcp.NewHandler(slog.LevelInfo))) }
8 changes: 5 additions & 3 deletions gcp/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ const LevelCritical = slog.Level(12)

// Handler that outputs JSON understood by the structured log agent.
// See https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
type Handler struct{ handler slog.Handler }
type Handler struct {
handler slog.Handler
}

func NewHandler() *Handler {
func NewHandler(level slog.Level) *Handler {
return &Handler{handler: slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
Level: level,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.MessageKey {
a.Key = "message"
Expand Down

0 comments on commit f42cf6d

Please sign in to comment.