Skip to content

Commit d0523d1

Browse files
committed
Add shim log options
Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
1 parent 052ce77 commit d0523d1

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

pkg/logging/logging.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,47 @@ import (
3131
"github.com/containerd/log"
3232
)
3333

34+
// SetupShimLogOption configures [SetupShimLog].
35+
type SetupShimLogOption func(*shimLogConfig)
36+
37+
type format int
38+
39+
const (
40+
formatText format = iota
41+
formatJSON
42+
)
43+
44+
type shimLogConfig struct {
45+
format format
46+
}
47+
48+
// WithJSONFormat sets the log output format to emit one JSON object per line.
49+
func WithJSONFormat() SetupShimLogOption {
50+
return func(c *shimLogConfig) {
51+
c.format = formatJSON
52+
}
53+
}
54+
3455
// SetupShimLog configures slog-based logging for the shim process.
3556
// It opens the platform-specific log output (FIFO on Unix, named pipe
36-
// on Windows), then creates a slog TextHandler and sets it as the
37-
// default logger with a "component=shim" attribute.
57+
// on Windows), then creates a slog handler and sets it as the default
58+
// logger with a "component=shim" attribute.
59+
//
60+
// The handler's format defaults to text format. Pass [WithJSONFormat]
61+
// to emit JSON, which structured log consumers can unwrap directly.
3862
//
3963
// The base handler (without component) is stored for use by
4064
// [ForwardConsoleLogs] so that forwarded records carry their own
4165
// component rather than inheriting "shim".
4266
//
4367
// For the short-lived start and delete actions, only [log.UseSlog] is
4468
// called to route logrus through slog; the log output is not opened.
45-
func SetupShimLog() {
69+
func SetupShimLog(opts ...SetupShimLogOption) {
70+
cfg := shimLogConfig{format: formatText}
71+
for _, o := range opts {
72+
o(&cfg)
73+
}
74+
4675
log.UseSlog()
4776

4877
var (
@@ -81,7 +110,15 @@ func SetupShimLog() {
81110
log.SetLevel("debug") //nolint:errcheck
82111
}
83112

84-
handler := slog.NewTextHandler(w, &slog.HandlerOptions{Level: &level}).WithAttrs(attrs)
113+
handlerOpts := &slog.HandlerOptions{Level: &level}
114+
var handler slog.Handler
115+
switch cfg.format {
116+
case formatJSON:
117+
handler = slog.NewJSONHandler(w, handlerOpts)
118+
default:
119+
handler = slog.NewTextHandler(w, handlerOpts)
120+
}
121+
handler = handler.WithAttrs(attrs)
85122
SetBaseHandler(handler)
86123
slog.SetDefault(slog.New(handler).With("component", "shim"))
87124
}

pkg/logging/logging_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ func TestForwardConsoleLogs_DebugLevel(t *testing.T) {
235235
}
236236
}
237237

238+
func TestWithJSONFormat(t *testing.T) {
239+
cfg := shimLogConfig{format: formatText}
240+
WithJSONFormat()(&cfg)
241+
if cfg.format != formatJSON {
242+
t.Errorf("WithJSONFormat: got %d, want %d", cfg.format, formatJSON)
243+
}
244+
}
245+
246+
func TestSetupShimLogDefaultIsText(t *testing.T) {
247+
// FormatText must be the zero value so that a shimLogConfig without
248+
// explicit initialisation defaults to text (backward-compatible).
249+
var cfg shimLogConfig
250+
if cfg.format != formatText {
251+
t.Errorf("zero-value shimLogConfig.format = %d, want FormatText (%d)", cfg.format, formatText)
252+
}
253+
}
254+
238255
func TestForwardJSONLog_InvalidJSON(t *testing.T) {
239256
SetBaseHandler(discardHandler{})
240257
if forwardJSONLog("{not json") {

0 commit comments

Comments
 (0)