-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add logging stack with slog adapter #1574
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
base: main
Are you sure you want to change the base?
Changes from all commits
4938a50
e10ff3e
cfda035
9af271a
d0e8fd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||
| package log | ||||||
|
|
||||||
| // Level represents the log level, from debug to fatal | ||||||
| type Level struct { | ||||||
| level string | ||||||
| } | ||||||
|
|
||||||
| var ( | ||||||
| // DebugLevel causes all logs to be logged | ||||||
| DebugLevel = Level{"debug"} | ||||||
| // InfoLevel causes all logs of level info or more severe to be logged | ||||||
| InfoLevel = Level{"info"} | ||||||
| // WarnLevel causes all logs of level warn or more severe to be logged | ||||||
| WarnLevel = Level{"warn"} | ||||||
| // ErrorLevel causes all logs of level error or more severe to be logged | ||||||
| ErrorLevel = Level{"error"} | ||||||
| // FatalLevel causes only logs of level fatal to be logged | ||||||
| FatalLevel = Level{"fatal"} | ||||||
| ) | ||||||
|
|
||||||
| // String returns the string representation for Level | ||||||
| // | ||||||
| // This is useful when trying to get the string values for Level and mapping level in other external libraries. For example: | ||||||
| // ``` | ||||||
| // trace.SetLogLevel(kvp.String("loglevel", log.DebugLevel.String()) | ||||||
|
||||||
| // trace.SetLogLevel(kvp.String("loglevel", log.DebugLevel.String()) | |
| // trace.SetLogLevel(kvp.String("loglevel", log.DebugLevel.String())) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package log | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| type Logger interface { | ||
| Log(ctx context.Context, level Level, msg string, fields ...slog.Attr) | ||
| Debug(msg string, fields ...slog.Attr) | ||
| Info(msg string, fields ...slog.Attr) | ||
| Warn(msg string, fields ...slog.Attr) | ||
| Error(msg string, fields ...slog.Attr) | ||
| Fatal(msg string, fields ...slog.Attr) | ||
| WithFields(fields ...slog.Attr) Logger | ||
| WithError(err error) Logger | ||
| Named(name string) Logger | ||
| WithLevel(level Level) Logger | ||
| Sync() error | ||
| Level() Level | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||
| package log | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "context" | ||||||||||
| "log/slog" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| type NoopLogger struct{} | ||||||||||
|
|
||||||||||
| var _ Logger = (*NoopLogger)(nil) | ||||||||||
|
|
||||||||||
| func NewNoopLogger() *NoopLogger { | ||||||||||
| return &NoopLogger{} | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Level() Level { | ||||||||||
| return DebugLevel | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Log(_ context.Context, _ Level, _ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Debug(_ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Info(_ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Warn(_ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Error(_ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (l *NoopLogger) Fatal(_ string, _ ...slog.Attr) { | ||||||||||
| // No-op | ||||||||||
|
Comment on lines
+40
to
+41
|
||||||||||
| func (l *NoopLogger) Fatal(_ string, _ ...slog.Attr) { | |
| // No-op | |
| func (l *NoopLogger) Fatal(msg string, _ ...slog.Attr) { | |
| panic("NoopLogger.Fatal called: " + msg) |
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.
Your a badass ai
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.
Your a badass ai
Copilot
AI
Dec 12, 2025
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.
The NoopLogger implementation lacks test coverage. While it's a simple no-op implementation, tests should verify that it correctly implements the Logger interface and that all methods are safe to call without side effects. Similar packages in this repository have corresponding test files.
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.
The comment for FatalLevel states "causes only logs of level fatal to be logged" which is inconsistent with other level comments. This suggests filtering that would suppress all other logs, but FatalLevel should be described as "causes all logs of level fatal to be logged" or "causes only fatal severity logs to be logged" to maintain consistency with the pattern of the other levels.