-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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 6 commits
4938a50
e10ff3e
cfda035
9af271a
d0e8fd9
7cc9b04
cfdf377
67c9327
88e323a
b67f2e1
dc00de6
26fa59d
bcd058a
195b0e5
919b184
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())) |
Copilot
AI
Jan 2, 2026
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 code example in the documentation is incomplete and contains a syntax error. The closing parenthesis is missing from the String() method call, and "kvp" is not defined in the example context. Consider revising to a complete, self-contained example.
| // ``` | |
| // trace.SetLogLevel(kvp.String("loglevel", log.DebugLevel.String()) | |
| // ``` | |
| // ```go | |
| // kvp := kv.String("loglevel", log.DebugLevel.String()) | |
| // trace.SetLogLevel(kvp) | |
| // ``` |
| 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 | ||
| } | ||
|
Comment on lines
+8
to
+21
|
||
| 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) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+11
|
||||||||||||||||||||||||||||
| type NoopLogger struct{} | |
| var _ Logger = (*NoopLogger)(nil) | |
| // NoopLogger is a Logger implementation that discards all log messages. | |
| // It is useful in tests or when logging is intentionally disabled. | |
| type NoopLogger struct{} | |
| var _ Logger = (*NoopLogger)(nil) | |
| // NewNoopLogger creates a NoopLogger that discards all log messages. | |
| // This is helpful for tests or environments where logging should be turned off. |
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's Fatal method should maintain consistent behavior with the SlogLogger implementation. Since SlogLogger panics after logging a fatal message, NoopLogger should also panic to ensure consistent Fatal semantics across implementations. Otherwise, code that relies on Fatal terminating execution could continue running unexpectedly when using NoopLogger.
| 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.