-
Notifications
You must be signed in to change notification settings - Fork 2k
Go: Model log/slog as a logging sink
#22004
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added models for the `log/slog` package (Go 1.21+). Its logging functions and | ||
| `*slog.Logger` methods (`Debug`/`Info`/`Warn`/`Error`, their `Context` | ||
| variants, and `Log`/`LogAttrs`) are now recognized as logging sinks, so the | ||
| `go/log-injection` and `go/clear-text-logging` queries cover code that logs | ||
| through `slog`. |
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 |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| extensions: | ||
| - addsTo: | ||
| pack: codeql/go-all | ||
| extensible: sinkModel | ||
| data: | ||
| # Package-level convenience functions (msg string, args ...any). | ||
| - ["log/slog", "", False, "Debug", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "Info", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "Warn", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "Error", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| # Context variants (ctx, msg string, args ...any). | ||
| - ["log/slog", "", False, "DebugContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "InfoContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "WarnContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "ErrorContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| # Log/LogAttrs (ctx, level, msg string, args/attrs ...). | ||
| - ["log/slog", "", False, "Log", "", "", "Argument[2..3]", "log-injection", "manual"] | ||
| - ["log/slog", "", False, "LogAttrs", "", "", "Argument[2..3]", "log-injection", "manual"] | ||
| # Methods on *slog.Logger. | ||
| - ["log/slog", "Logger", True, "Debug", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "Info", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "Warn", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "Error", "", "", "Argument[0..1]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "DebugContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "InfoContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "WarnContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "ErrorContext", "", "", "Argument[1..2]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "Log", "", "", "Argument[2..3]", "log-injection", "manual"] | ||
| - ["log/slog", "Logger", True, "LogAttrs", "", "", "Argument[2..3]", "log-injection", "manual"] |
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,6 +1,6 @@ | ||
| module codeql-go-tests/concepts/loggercall | ||
|
|
||
| go 1.15 | ||
| go 1.21 | ||
|
|
||
| require ( | ||
| github.com/golang/glog v1.2.5 | ||
|
|
||
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
39 changes: 39 additions & 0 deletions
39
go/ql/test/library-tests/semmle/go/concepts/LoggerCall/slog.go
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| func slogTest() { | ||
| ctx := context.Background() | ||
| var logger *slog.Logger | ||
|
|
||
| // Methods on *slog.Logger: Debug/Info/Warn/Error(msg string, args ...any). | ||
| logger.Debug(text) // $ logger=text | ||
| logger.Info(text) // $ logger=text | ||
| logger.Warn(text) // $ logger=text | ||
| logger.Error(text) // $ logger=text | ||
| logger.Info(text, key, v) // $ logger=text logger=key logger=v | ||
|
|
||
| // Context variants: (ctx, msg string, args ...any). | ||
| logger.DebugContext(ctx, text) // $ logger=text | ||
| logger.InfoContext(ctx, text) // $ logger=text | ||
| logger.WarnContext(ctx, text) // $ logger=text | ||
| logger.ErrorContext(ctx, text) // $ logger=text | ||
| logger.InfoContext(ctx, text, key, v) // $ logger=text logger=key logger=v | ||
|
|
||
| // Log/LogAttrs: (ctx, level, msg string, args/attrs ...). | ||
| logger.Log(ctx, slog.LevelInfo, text, key, v) // $ logger=text logger=key logger=v | ||
| logger.LogAttrs(ctx, slog.LevelInfo, text) // $ logger=text | ||
|
|
||
| // Package-level convenience functions. | ||
| slog.Debug(text) // $ logger=text | ||
| slog.Info(text) // $ logger=text | ||
| slog.Warn(text) // $ logger=text | ||
| slog.Error(text) // $ logger=text | ||
| slog.Info(text, key, v) // $ logger=text logger=key logger=v | ||
| slog.InfoContext(ctx, text, key, v) // $ logger=text logger=key logger=v | ||
| slog.Log(ctx, slog.LevelInfo, text) // $ logger=text | ||
| slog.LogAttrs(ctx, slog.LevelInfo, text) // $ logger=text | ||
|
sauyon marked this conversation as resolved.
Outdated
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.