|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package logging |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "log/slog" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// Format represents the log output format. |
| 14 | +type Format int |
| 15 | + |
| 16 | +const ( |
| 17 | + // FormatJSON produces JSON-formatted log output using [log/slog.JSONHandler]. |
| 18 | + // This is the default format, suitable for production environments. |
| 19 | + FormatJSON Format = iota |
| 20 | + |
| 21 | + // FormatText produces human-readable text output using [log/slog.TextHandler]. |
| 22 | + // This is suitable for local development. |
| 23 | + FormatText |
| 24 | +) |
| 25 | + |
| 26 | +// config holds the resolved configuration for creating a logger. |
| 27 | +type config struct { |
| 28 | + format Format |
| 29 | + level slog.Leveler |
| 30 | + output io.Writer |
| 31 | +} |
| 32 | + |
| 33 | +// Option configures the logger created by [New]. |
| 34 | +type Option func(*config) |
| 35 | + |
| 36 | +// WithFormat sets the output format (JSON or Text). |
| 37 | +// The default is [FormatJSON]. |
| 38 | +func WithFormat(f Format) Option { |
| 39 | + return func(c *config) { |
| 40 | + c.format = f |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// WithLevel sets the minimum log level. |
| 45 | +// The default is [log/slog.LevelInfo]. |
| 46 | +// |
| 47 | +// Accepts any [log/slog.Leveler], including [*log/slog.LevelVar] for |
| 48 | +// dynamic level changes: |
| 49 | +// |
| 50 | +// var lvl slog.LevelVar |
| 51 | +// lvl.Set(slog.LevelDebug) |
| 52 | +// logger := logging.New(logging.WithLevel(&lvl)) |
| 53 | +func WithLevel(l slog.Leveler) Option { |
| 54 | + return func(c *config) { |
| 55 | + c.level = l |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// WithOutput sets the destination writer for log output. |
| 60 | +// The default is [os.Stderr]. |
| 61 | +func WithOutput(w io.Writer) Option { |
| 62 | + return func(c *config) { |
| 63 | + c.output = w |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// New creates a pre-configured [*log/slog.Logger] with consistent defaults |
| 68 | +// used across the ToolHive ecosystem. |
| 69 | +// |
| 70 | +// Defaults: |
| 71 | +// - Format: JSON ([FormatJSON]) |
| 72 | +// - Level: INFO ([log/slog.LevelInfo]) |
| 73 | +// - Output: [os.Stderr] |
| 74 | +// - Timestamps: [time.RFC3339] |
| 75 | +func New(opts ...Option) *slog.Logger { |
| 76 | + cfg := &config{ |
| 77 | + format: FormatJSON, |
| 78 | + level: slog.LevelInfo, |
| 79 | + output: os.Stderr, |
| 80 | + } |
| 81 | + |
| 82 | + for _, opt := range opts { |
| 83 | + opt(cfg) |
| 84 | + } |
| 85 | + |
| 86 | + handlerOpts := &slog.HandlerOptions{ |
| 87 | + Level: cfg.level, |
| 88 | + ReplaceAttr: replaceAttr, |
| 89 | + } |
| 90 | + |
| 91 | + var handler slog.Handler |
| 92 | + switch cfg.format { |
| 93 | + case FormatText: |
| 94 | + handler = slog.NewTextHandler(cfg.output, handlerOpts) |
| 95 | + case FormatJSON: |
| 96 | + handler = slog.NewJSONHandler(cfg.output, handlerOpts) |
| 97 | + } |
| 98 | + |
| 99 | + return slog.New(handler) |
| 100 | +} |
| 101 | + |
| 102 | +// replaceAttr formats the time attribute to RFC3339. |
| 103 | +// All other attributes are passed through unchanged. |
| 104 | +func replaceAttr(_ []string, a slog.Attr) slog.Attr { |
| 105 | + if a.Key == slog.TimeKey { |
| 106 | + if t, ok := a.Value.Any().(time.Time); ok { |
| 107 | + a.Value = slog.StringValue(t.Format(time.RFC3339)) |
| 108 | + } |
| 109 | + } |
| 110 | + return a |
| 111 | +} |
0 commit comments