-
Notifications
You must be signed in to change notification settings - Fork 105
GCI1066 [Team TREE][2025] - Logging format interpolation #404
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
Open
NETOIK
wants to merge
9
commits into
green-code-initiative:main
Choose a base branch
from
NETOIK:LoggingFormatInterpolation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2057637
Update CHANGELOG.md
NETOIK 7eaabb2
Update RULES.md
NETOIK 81b758a
Add rule specification
NETOIK 6c1f6eb
update md files
NETOIK 19ec6db
Add sub folder python
NETOIK 7165544
Fix plot yTitle
NETOIK f0d9891
Fix image
NETOIK d014523
Fix typo
NETOIK 3bfdcba
Add tag python
NETOIK 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
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
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,17 @@ | ||
| { | ||
| "title": "Logging Format Interpolation - Avoid using immediate string formatter in logging calls.", | ||
| "type": "CODE_SMELL", | ||
| "status": "ready", | ||
| "remediation": { | ||
| "func": "Constant\/Issue", | ||
| "constantCost": "5min" | ||
| }, | ||
| "tags": [ | ||
| "creedengo", | ||
| "eco-design", | ||
| "performance", | ||
| "logging", | ||
| "format" | ||
| ], | ||
| "defaultSeverity": "Minor" | ||
| } | ||
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,51 @@ | ||
| When logging with formatted string, prefer using `%s` and logging kwargs which will be deferred until it cannot be avoided, instead of immediate `.format()` of `f""` interpolation. | ||
|
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. Could you add a new test with codeCarbon to check these to uses cases :
|
||
|
|
||
| == Non Compliant Code Example | ||
|
|
||
| [source,python] | ||
| ---- | ||
| import logging | ||
| logging.basicConfig(level=logging.INFO) | ||
| name = "world" | ||
| logging.debug(f"hello {world}") # Non Compliant: the replacement will be done immediately but will not be printed because current level is INFO | ||
| ---- | ||
|
|
||
| == Compliant Solution | ||
|
|
||
| [source,python] | ||
| ---- | ||
| import logging | ||
| logging.basicConfig(level=logging.INFO) | ||
| name = "world" | ||
| logging.debug("hello %s", name) # Compliant: the replacement will be avoided by logging module because it is not necessary | ||
| ---- | ||
|
|
||
|
|
||
| == Relevance Analysis | ||
|
|
||
| This rule is relevant to logging formatting. Using logging kwargs is more efficient than immediate builtin formatting. | ||
|
|
||
| === Configuration | ||
|
|
||
| * Processor: Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz, 4 cores | ||
| * RAM: 8 GB | ||
| * CO2 Emissions Measurement: Using https://mlco2.github.io/codecarbon/[CodeCarbon] | ||
|
|
||
| === Context | ||
|
|
||
| Two approaches were benchmarked: | ||
| - *Non-compliant:* Using logging with `.format()` | ||
| - *Compliant:* Using logging with `%s` and kwargs | ||
|
|
||
| === Impact Analysis | ||
|
|
||
| image::results.png[] | ||
NETOIK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| == Conclusion | ||
|
|
||
| Replacing `f""` and `"".format()` with `%s` and logging kwargs permits to defer template replacement when the log is not displayed. | ||
|
|
||
| == References | ||
|
|
||
| - https://docs.python.org/3/howto/logging.html#optimization | ||
| - https://pylint.pycqa.org/en/latest/user_guide/messages/warning/logging-format-interpolation.html | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.