-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bd4117
commit 6b40c26
Showing
6 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,34 @@ | ||
from core_codemods.lazy_logging import LazyLogging | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestLazyLogging(BaseIntegrationTest): | ||
codemod = LazyLogging | ||
code_path = "tests/samples/lazy_logging.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
(2, """logging.error("Error occurred: %s", e)\n"""), | ||
(3, """logging.error("Error occurred: %s", e)\n"""), | ||
], | ||
) | ||
|
||
# fmt: off | ||
expected_diff =( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -1,4 +1,4 @@\n""" | ||
""" import logging\n""" | ||
""" e = "Some error"\n""" | ||
"""-logging.error("Error occurred: %s" % e)\n""" | ||
"""-logging.error("Error occurred: " + e)\n""" | ||
"""+logging.error("Error occurred: %s", e)\n""" | ||
"""+logging.error("Error occurred: %s", e)\n""") | ||
# fmt: on | ||
|
||
expected_line_change = "3" | ||
change_description = LazyLogging.change_description | ||
num_changes = 2 |
This file contains 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 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 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,13 @@ | ||
This codemod converts "eager" logging into "lazy" logging, which is preferred for performance efficiency and resource optimization. | ||
Lazy logging defers the actual construction and formatting of log messages until it's confirmed that the message will be logged based on the current log level, thereby avoiding unnecessary computation and memory usage for messages that will be ignored. | ||
|
||
Our changes look something like this: | ||
|
||
```diff | ||
import logging | ||
e = "Some error" | ||
- logging.error("Error occurred: %s" % e) | ||
- logging.error("Error occurred: " + e) | ||
+ logging.error("Error occurred: %s", e) | ||
+ logging.error("Error occurred: %s", e) | ||
``` |
This file contains 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 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,4 @@ | ||
import logging | ||
e = "Some error" | ||
logging.error("Error occurred: %s" % e) | ||
logging.error("Error occurred: " + e) |