Skip to content

Commit 9d83f3d

Browse files
Seppli11sonartech
authored andcommitted
SONARPY-4136 Remove S8554 from SonarWay (#1094)
GitOrigin-RevId: 2df1c5931179d3d38d450fc936ab0412a97367d6
1 parent 3825a61 commit 9d83f3d

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S8554.html

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,32 @@
33
<h2>Why is this an issue?</h2>
44
<p>Python’s logging module has specific patterns that ensure correctness, performance, and maintainability. Violating these patterns can lead to
55
several problems:</p>
6-
<h2>Eager String Formatting</h2>
6+
<h3>Eager String Formatting</h3>
77
<p>When you format strings before passing them to logging methods (using f-strings, <code>.format()</code>, <code>%</code> operator, or <code>+</code>
88
concatenation), Python performs the string formatting operation immediately, even if the log message won’t be output due to the current log level.
99
This wastes CPU cycles on string operations that may never be used.</p>
1010
<p>The logging module supports lazy formatting by accepting format arguments as separate parameters. The formatting only occurs when the logging
1111
framework confirms the message will actually be logged based on the configured log level.</p>
12-
<h2>Deprecated Methods</h2>
12+
<h3>Deprecated Methods</h3>
1313
<p>The <code>logging.warn()</code> and <code>logging.Logger.warn()</code> methods are deprecated. While they still function, they may be removed in
1414
future Python versions. The recommended methods are <code>logging.warning()</code> and <code>logging.Logger.warning()</code>, which are functionally
1515
equivalent.</p>
16-
<h2>Attribute Name Collisions</h2>
16+
<h3>Attribute Name Collisions</h3>
1717
<p>The logging module’s <code>extra</code> keyword argument allows passing additional values to be included in log records. However, if the keys in
1818
the <code>extra</code> dictionary clash with built-in <code>LogRecord</code> attributes (such as 'name', 'msg', 'args', 'created', 'filename',
1919
'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'pathname', 'process', 'processName', 'relativeCreated', 'thread',
2020
'threadName'), a <code>KeyError</code> will be raised when the <code>LogRecord</code> is constructed, causing the logging statement to fail at
2121
runtime.</p>
2222
<h3>What is the potential impact?</h3>
23-
<h2>Performance Degradation</h2>
24-
<p>Eager string formatting can significantly impact application performance, especially in code paths with frequent logging statements. When log
25-
levels are configured to filter out certain messages (e.g., DEBUG messages in production), the application still pays the cost of formatting strings
26-
that will never be logged.</p>
27-
<h2>Runtime Errors</h2>
28-
<p>Attribute name collisions in the <code>extra</code> parameter raise <code>KeyError</code> exceptions, causing logging statements to fail
29-
unexpectedly.</p>
30-
<h2>Maintenance Issues</h2>
31-
<p>Using deprecated methods creates technical debt. When these methods are eventually removed from Python, code using them will break. Additionally,
32-
mixing deprecated and non-deprecated methods across a codebase creates inconsistency and confusion.</p>
33-
<h2>Inconsistent Logging</h2>
34-
<p>Eager formatting can lead to inconsistent behavior across different log handlers. When using lazy formatting with the <code>extra</code> parameter,
35-
all handlers receive the same structured data and can format it consistently according to their configuration.</p>
23+
<p>Performance degradation: Eager string formatting can significantly impact application performance, especially in code paths with frequent logging
24+
statements. When log levels are configured to filter out certain messages (for example, DEBUG messages in production), the application still pays the
25+
cost of formatting strings that will never be logged.</p>
26+
<p>Runtime errors: Attribute name collisions in the <code>extra</code> parameter raise <code>KeyError</code> exceptions, causing logging statements to
27+
fail unexpectedly.</p>
28+
<p>Maintenance issues: Using deprecated methods creates technical debt. When these methods are eventually removed from Python, code using them will
29+
break. Additionally, mixing deprecated and non-deprecated methods across a codebase creates inconsistency and confusion.</p>
30+
<p>Inconsistent logging: Eager formatting can lead to inconsistent behavior across different log handlers. When using lazy formatting with the
31+
<code>extra</code> parameter, all handlers receive the same structured data and can format it consistently according to their configuration.</p>
3632
<h2>How to fix it</h2>
3733
<p>Use lazy formatting by passing format arguments as separate parameters to logging methods instead of formatting strings before the call. Replace
3834
f-strings, <code>.format()</code>, <code>%</code> operator, and <code>+</code> concatenation with %-style placeholders and separate arguments.</p>

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/Sonar_way_profile.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@
345345
"S8519",
346346
"S8520",
347347
"S8521",
348-
"S8554",
349348
"S8572",
350349
"S8685"
351350
]

0 commit comments

Comments
 (0)