Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,47 @@ import (
"github.com/containerd/log"
)

// SetupShimLogOption configures [SetupShimLog].
type SetupShimLogOption func(*shimLogConfig)

type format int

const (
formatText format = iota
formatJSON
)

type shimLogConfig struct {
format format
}

// WithJSONFormat sets the log output format to emit one JSON object per line.
func WithJSONFormat() SetupShimLogOption {
return func(c *shimLogConfig) {
c.format = formatJSON
}
}

// SetupShimLog configures slog-based logging for the shim process.
// It opens the platform-specific log output (FIFO on Unix, named pipe
// on Windows), then creates a slog TextHandler and sets it as the
// default logger with a "component=shim" attribute.
// on Windows), then creates a slog handler and sets it as the default
// logger with a "component=shim" attribute.
//
// The handler's format defaults to text format. Pass [WithJSONFormat]
// to emit JSON, which structured log consumers can unwrap directly.
//
Comment thread
austinvazquez marked this conversation as resolved.
// The base handler (without component) is stored for use by
// [ForwardConsoleLogs] so that forwarded records carry their own
// component rather than inheriting "shim".
//
// For the short-lived start and delete actions, only [log.UseSlog] is
// called to route logrus through slog; the log output is not opened.
func SetupShimLog() {
func SetupShimLog(opts ...SetupShimLogOption) {
cfg := shimLogConfig{format: formatText}
for _, o := range opts {
o(&cfg)
}

log.UseSlog()

var (
Expand Down Expand Up @@ -81,7 +110,15 @@ func SetupShimLog() {
log.SetLevel("debug") //nolint:errcheck
}

handler := slog.NewTextHandler(w, &slog.HandlerOptions{Level: &level}).WithAttrs(attrs)
handlerOpts := &slog.HandlerOptions{Level: &level}
var handler slog.Handler
switch cfg.format {
case formatJSON:
handler = slog.NewJSONHandler(w, handlerOpts)
default:
handler = slog.NewTextHandler(w, handlerOpts)
}
handler = handler.WithAttrs(attrs)
SetBaseHandler(handler)
slog.SetDefault(slog.New(handler).With("component", "shim"))
Comment thread
austinvazquez marked this conversation as resolved.
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ func TestForwardConsoleLogs_DebugLevel(t *testing.T) {
}
}

func TestWithJSONFormat(t *testing.T) {
cfg := shimLogConfig{format: formatText}
WithJSONFormat()(&cfg)
if cfg.format != formatJSON {
t.Errorf("WithJSONFormat: got %d, want %d", cfg.format, formatJSON)
}
}

func TestSetupShimLogDefaultIsText(t *testing.T) {
// formatText must be the zero value so that a shimLogConfig without
// explicit initialisation defaults to text (backward-compatible).
var cfg shimLogConfig
if cfg.format != formatText {
t.Errorf("zero-value shimLogConfig.format = %d, want formatText (%d)", cfg.format, formatText)
}
}

func TestForwardJSONLog_InvalidJSON(t *testing.T) {
SetBaseHandler(discardHandler{})
if forwardJSONLog("{not json") {
Expand Down