-
Notifications
You must be signed in to change notification settings - Fork 354
refactor: replace logrus for global slog logger #889
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
Changes from 22 commits
851e42f
7a0d52f
0c99916
b98c85b
530d0ce
586fdee
49353b6
2facbb6
fc2c5f6
4eadd9f
b2c88d2
7f23a13
82d98d5
1064fd2
49405a2
6c29f67
2ce403c
1a5537f
74824b3
64f9b21
1236b9c
6861c47
853ae6e
2e0ed68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package log // import "go.opentelemetry.io/ebpf-profiler/internal/log" | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "sync/atomic" | ||
| ) | ||
|
|
||
| // globalLogger holds a reference to the [slog.Logger] used within | ||
| // go.opentelemetry.io/ebpf-profiler. | ||
| // | ||
| // The default logger logs to stderr which is backed by the standard `log.Logger` | ||
| // interface. This logger will show messages at the Info Level. | ||
| var globalLogger = func() *atomic.Pointer[slog.Logger] { | ||
| l := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ | ||
| Level: slog.LevelInfo, | ||
| })) | ||
|
|
||
| p := new(atomic.Pointer[slog.Logger]) | ||
| p.Store(l) | ||
| return p | ||
| }() | ||
|
|
||
| // SetLogger sets the global Logger to l. | ||
| func SetLogger(l slog.Logger) { | ||
| globalLogger.Store(&l) | ||
| } | ||
|
|
||
| // SetDebugLogger configures the global logger to write debug-level logs to stderr. | ||
|
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. Instead of
Contributor
Author
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. The Also, keep in mind that this will be removed once we transition to just having the otel receiver (verbosity will be defined from the collector's configuration)
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. In the example provided with slog.SetLogLoggerLevel, changing the severity level after setting the logger just works.
Contributor
Author
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. That function only works in case we were using the default |
||
| func SetDebugLogger() { | ||
| SetLogger(*slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{ | ||
| Level: slog.LevelDebug, | ||
| }))) | ||
| } | ||
|
|
||
| // getLogger returns the global logger. | ||
| func getLogger() *slog.Logger { | ||
| return globalLogger.Load() | ||
| } | ||
|
|
||
| // Infof logs informational messages about the general state of the profiler. | ||
| // This function is a wrapper around the structured slog-based logger, | ||
| // formatting the message as a string for backward compatibility with | ||
| // previous unstructured logging. | ||
| func Infof(msg string, keysAndValues ...any) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelInfo) { | ||
| getLogger().Info(fmt.Sprintf(msg, keysAndValues...)) | ||
| } | ||
| } | ||
|
|
||
| // Info logs informational messages about the general state of the profiler. | ||
| // This is a wrapper around Infof for convenience. | ||
| func Info(msg string) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelInfo) { | ||
| getLogger().Info(msg) | ||
| } | ||
| } | ||
|
|
||
| // Errorf logs error messages about exceptional states of the profiler. | ||
| // This wrapper formats structured log data into a string message for | ||
| // backward compatibility with older unstructured logs. | ||
| func Errorf(msg string, keysAndValues ...any) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelError) { | ||
| getLogger().Error(fmt.Sprintf(msg, keysAndValues...)) | ||
| } | ||
| } | ||
|
|
||
| // Error logs error messages about exceptional states of the profiler. | ||
| // This is a wrapper around Errorf for convenience. | ||
| func Error(msg error) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelError) { | ||
| getLogger().Error(msg.Error()) | ||
| } | ||
| } | ||
|
|
||
| // Debugf logs detailed debugging information about internal profiler behavior. | ||
| // This wrapper converts structured log data into a string message for | ||
| // backward compatibility with older unstructured logs. | ||
| func Debugf(msg string, keysAndValues ...any) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelDebug) { | ||
| getLogger().Debug(fmt.Sprintf(msg, keysAndValues...)) | ||
| } | ||
| } | ||
|
|
||
| // Debug logs detailed debugging information about internal profiler behavior. | ||
| // This is a wrapper around Debugf for convenience. | ||
| func Debug(msg string) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelDebug) { | ||
| getLogger().Debug(msg) | ||
| } | ||
| } | ||
|
|
||
| // Warnf logs warnings in the profiler — not errors, but likely more important | ||
| // than informational messages. This wrapper preserves backward compatibility | ||
| // by string-formatting structured log data. | ||
| func Warnf(msg string, keysAndValues ...any) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelWarn) { | ||
| getLogger().Warn(fmt.Sprintf(msg, keysAndValues...)) | ||
| } | ||
| } | ||
|
|
||
| // Warn logs warnings in the profiler — not errors, but likely more important | ||
| // than informational messages. This is a wrapper around Warnf for convenience. | ||
| func Warn(msg string) { | ||
| if getLogger().Enabled(context.Background(), slog.LevelWarn) { | ||
| getLogger().Warn(msg) | ||
| } | ||
| } | ||
|
|
||
| // Fatalf logs a fatal error message and exits the program. | ||
| // This wrapper maintains backward compatibility with unstructured logs by | ||
| // formatting messages as strings. | ||
| // TODO: remove Fatalf calls from the codebase (https://github.com/open-telemetry/opentelemetry-ebpf-profiler/issues/888). | ||
| func Fatalf(msg string, keysAndValues ...any) { | ||
| Errorf(msg, keysAndValues...) | ||
| os.Exit(1) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.