Skip to content

Commit

Permalink
document lazy logging codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jan 29, 2024
1 parent 3bd4117 commit 6b40c26
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
34 changes: 34 additions & 0 deletions integration_tests/test_lazy_logging.py
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
4 changes: 4 additions & 0 deletions src/codemodder/scripts/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ class DocMetadata:
importance="Medium",
guidance_explained="An `assert` statement on a non-empty tuple is likely unintended and should be rewritten. However, the new change may result in assertion failures that should be reviewed.",
),
"lazy-logging": DocMetadata(
importance="Medium",
guidance_explained="We believe this change is safe and will not cause any issues.",
),
}

METADATA = CORE_METADATA | {
Expand Down
3 changes: 2 additions & 1 deletion src/core_codemods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
from .fix_empty_sequence_comparison import FixEmptySequenceComparison
from .remove_assertion_in_pytest_raises import RemoveAssertionInPytestRaises
from .fix_assert_tuple import FixAssertTuple

from .sonar.sonar_numpy_nan_equality import SonarNumpyNanEquality
from .lazy_logging import LazyLogging

registry = CodemodCollection(
origin="pixee",
Expand Down Expand Up @@ -106,6 +106,7 @@
FixEmptySequenceComparison,
RemoveAssertionInPytestRaises,
FixAssertTuple,
LazyLogging,
],
)

Expand Down
13 changes: 13 additions & 0 deletions src/core_codemods/docs/pixee_python_lazy-logging.md
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)
```
2 changes: 1 addition & 1 deletion src/core_codemods/lazy_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LazyLogging(SimpleCodemod, NameAndAncestorResolutionMixin):
metadata = Metadata(
name="lazy-logging",
summary="Simplify Boolean Expressions Using `startswith` and `endswith`",
summary="Convert Eager Logging to Lazy Logging",
review_guidance=ReviewGuidance.MERGE_WITHOUT_REVIEW,
references=[],
)
Expand Down
4 changes: 4 additions & 0 deletions tests/samples/lazy_logging.py
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)

0 comments on commit 6b40c26

Please sign in to comment.