-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(gologshim): Add SetDefaultHandler #3418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b69d106
fix: regression in go-log interop after slog migration
lidel ea2c010
fix: unused parameter lint errors
lidel 885650f
fixup! review nits
MarcoPolo 789d14c
gologshim: allow customizing underlying handler at runtime
MarcoPolo 326593e
docs(gologshim): improve godoc clarity
lidel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,36 @@ | ||
| // Package gologshim provides slog-based logging that integrates with go-log | ||
| // when available, without requiring go-log as a dependency. | ||
| // | ||
| // Usage: | ||
| // | ||
| // var log = logging.Logger("subsystem") | ||
| // log.Debug("message", "key", "value") | ||
| // | ||
| // When go-log's slog bridge is detected (via duck typing), gologshim | ||
| // automatically integrates for unified formatting and dynamic level control. | ||
| // Otherwise, it creates standalone slog handlers writing to stderr. | ||
| // | ||
| // Environment variables (see go-log README for full details): | ||
| // - GOLOG_LOG_LEVEL: Set log levels per subsystem (e.g., "error,ping=debug") | ||
| // - GOLOG_LOG_FORMAT/GOLOG_LOG_FMT: Output format ("json" or text) | ||
| // - GOLOG_LOG_ADD_SOURCE: Include source location (default: true) | ||
| // - GOLOG_LOG_LABELS: Add key=value labels to all logs | ||
| // | ||
| // For integration details, see: https://github.com/ipfs/go-log/blob/master/README.md#slog-integration | ||
| // | ||
| // Note: This package exists as an intermediate solution while go-log uses zap | ||
| // internally. If go-log migrates from zap to native slog, this bridge layer | ||
| // could be simplified or removed entirely. | ||
| package gologshim | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| var lvlToLower = map[slog.Level]slog.Value{ | ||
|
|
@@ -15,25 +40,48 @@ var lvlToLower = map[slog.Level]slog.Value{ | |
| slog.LevelError: slog.StringValue("error"), | ||
| } | ||
|
|
||
| // Logger returns a *slog.Logger with a logging level defined by the | ||
| // GOLOG_LOG_LEVEL env var. Supports different levels for different systems. e.g. | ||
| // GOLOG_LOG_LEVEL=foo=info,bar=debug,warn | ||
| // sets the foo system at level info, the bar system at level debug and the | ||
| // fallback level to warn. | ||
| // | ||
| // Prefer a parameterized logger over a global logger. | ||
| func Logger(system string) *slog.Logger { | ||
| var h slog.Handler | ||
| c := ConfigFromEnv() | ||
| handlerOpts := &slog.HandlerOptions{ | ||
| Level: c.LevelForSystem(system), | ||
| AddSource: c.addSource, | ||
| // goLogBridge detects go-log's slog bridge via duck typing, without import dependency | ||
| type goLogBridge interface { | ||
| GoLogBridge() | ||
| } | ||
|
|
||
| // lazyBridgeHandler delays bridge detection until first log call to handle init order issues | ||
| type lazyBridgeHandler struct { | ||
| system string | ||
| config *Config | ||
| once sync.Once | ||
| handler atomic.Pointer[slog.Handler] | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) ensureHandler() slog.Handler { | ||
| if handler := h.handler.Load(); handler != nil { | ||
| return *handler | ||
| } | ||
|
|
||
| h.once.Do(func() { | ||
| var handler slog.Handler | ||
| if bridge, ok := slog.Default().Handler().(goLogBridge); ok { | ||
| attrs := make([]slog.Attr, 0, 1+len(h.config.labels)) | ||
| attrs = append(attrs, slog.String("logger", h.system)) | ||
| attrs = append(attrs, h.config.labels...) | ||
| handler = bridge.(slog.Handler).WithAttrs(attrs) | ||
| } else { | ||
| handler = h.createFallbackHandler() | ||
| } | ||
| h.handler.Store(&handler) | ||
| }) | ||
|
|
||
| return *h.handler.Load() | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) createFallbackHandler() slog.Handler { | ||
| opts := &slog.HandlerOptions{ | ||
| Level: h.config.LevelForSystem(h.system), | ||
| AddSource: h.config.addSource, | ||
| ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { | ||
| if a.Key == slog.TimeKey { | ||
| // ipfs go-log uses "ts" for time | ||
| a.Key = "ts" | ||
| } else if a.Key == slog.LevelKey { | ||
| // ipfs go-log uses lowercase level names | ||
| if lvl, ok := a.Value.Any().(slog.Level); ok { | ||
| if s, ok := lvlToLower[lvl]; ok { | ||
| a.Value = s | ||
|
|
@@ -43,16 +91,56 @@ func Logger(system string) *slog.Logger { | |
| return a | ||
| }, | ||
| } | ||
| if c.format == logFormatText { | ||
| h = slog.NewTextHandler(os.Stderr, handlerOpts) | ||
| var handler slog.Handler | ||
| if h.config.format == logFormatText { | ||
| handler = slog.NewTextHandler(os.Stderr, opts) | ||
| } else { | ||
| h = slog.NewJSONHandler(os.Stderr, handlerOpts) | ||
| handler = slog.NewJSONHandler(os.Stderr, opts) | ||
| } | ||
| attrs := make([]slog.Attr, 1+len(h.config.labels)) | ||
| attrs[0] = slog.String("logger", h.system) | ||
| copy(attrs[1:], h.config.labels) | ||
| return handler.WithAttrs(attrs) | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) Enabled(ctx context.Context, lvl slog.Level) bool { | ||
| return h.ensureHandler().Enabled(ctx, lvl) | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) Handle(ctx context.Context, r slog.Record) error { | ||
| return h.ensureHandler().Handle(ctx, r) | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
| return h.ensureHandler().WithAttrs(attrs) | ||
| } | ||
|
|
||
| func (h *lazyBridgeHandler) WithGroup(name string) slog.Handler { | ||
| return h.ensureHandler().WithGroup(name) | ||
| } | ||
|
|
||
| // Logger returns a *slog.Logger with a logging level defined by the | ||
| // GOLOG_LOG_LEVEL env var. Supports different levels for different systems. e.g. | ||
| // GOLOG_LOG_LEVEL=foo=info,bar=debug,warn | ||
| // sets the foo system at level info, the bar system at level debug and the | ||
| // fallback level to warn. | ||
| func Logger(system string) *slog.Logger { | ||
| c := ConfigFromEnv() | ||
|
|
||
| // If go-log bridge available, use it immediately | ||
| if _, ok := slog.Default().Handler().(goLogBridge); ok { | ||
| attrs := make([]slog.Attr, 0, 1+len(c.labels)) | ||
| attrs = append(attrs, slog.String("logger", system)) | ||
| attrs = append(attrs, c.labels...) | ||
| h := slog.Default().Handler().WithAttrs(attrs) | ||
| return slog.New(h) | ||
| } | ||
| attrs := make([]slog.Attr, 1+len(c.labels)) | ||
| attrs = append(attrs, slog.String("logger", system)) | ||
| attrs = append(attrs, c.labels...) | ||
| h = h.WithAttrs(attrs) | ||
| return slog.New(h) | ||
|
|
||
| // Use lazy handler for init order issues | ||
| return slog.New(&lazyBridgeHandler{ | ||
| system: system, | ||
| config: c, | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, cannot rely on |
||
| } | ||
|
|
||
| type logFormat = int | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ this is the gist of the solution that avoids depending on go-log in go.mod