Skip to content

Refactor: Use Standard OpenRewrite Recipes to implement Slf4j logging automatically #48

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

Draft
wants to merge 19 commits into
base: dev
Choose a base branch
from

Conversation

mcook42
Copy link
Contributor

@mcook42 mcook42 commented Apr 6, 2025

Description

Use OpenRewrite to replace direct system calls with structured logging. This will significantly improve maintainers' ability to understand and debug program execution. The only manual edits were made in the tests. All other code changes were automatically performed by their respective OpenRewrite Recipes using the Maven plugin.

How

  1. Removed the two overloaded methods logWithDate and inlined their System.out.println calls (necessary prep for the next step)
  2. Run https://docs.openrewrite.org/recipes/java/logging/systemprinttologging to replace system calls with logger calls
  3. Run https://docs.openrewrite.org/recipes/java/migrate/lombok/log/useslf4j to use @Slf4j annotations instead of explicit properties
  4. Run https://docs.openrewrite.org/recipes/java/logging/slf4j/slf4jbestpractices to implement best practices for Slf4j
  5. Tweak existing unit tests that relied on log inspection.

mcook42 added 15 commits April 5, 2025 19:54
Replaced `System.out.println` with proper logging using `log.error` to ensure exceptions are logged consistently and include stack trace details. This improves debugging and aligns with standard logging practices.
Eliminated unnecessary `verify(mockUtility)` calls from unit tests to improve clarity and maintain focus on actual test assertions. This helps streamline the code and reduce noise in test output.
Eliminated unnecessary `verify(mockUtility)` calls from unit tests to improve clarity and maintain focus on actual test assertions. This helps streamline the code and reduce noise in test output.
Eliminated unnecessary `verify(mockUtility)` calls from unit tests to improve clarity and maintain focus on actual test assertions. This helps streamline the code and reduce noise in test output.
Eliminated unnecessary `verify(mockUtility)` calls from unit tests to improve clarity and maintain focus on actual test assertions. This helps streamline the code and reduce noise in test output.
Log message assertions were removed as they provide limited value and rely on inspecting logs, which is discouraged. A low-value test was disabled due to its dependency on log messages without meaningful functional validation.
Removed unnecessary log and verify statements from CertExpirationConsumerTest to improve code clarity and maintainability. These changes help streamline test assertions without impacting functionality.
Added SLF4J best practices recipe to enforce proper logging conventions. Updated Maven dependencies and configuration to include the new rewrite-logging-frameworks artifact.
…actices to implement best practices

Replaced `log.info` with appropriate `log.warn` or `log.error` in error-prone scenarios and updated parameterized logging to use placeholders instead of string formatting. This improves log readability, corrects log levels, and standardizes logging practices across the codebase.
verify(mockUtility, times(3)).logWithDate(logMessageCaptor.capture(), eq(ValidateTmdd.class));
Assertions.assertEquals("Error sending summary email:", logMessageCaptor.getAllValues().get(1));
Assertions.assertEquals("Failed to send error summary email:", logMessageCaptor.getAllValues().get(2));
// TODO: how to assert behavior without inspecting logs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this TODO is intentional. The value of this automated refactor is to get more useful and detailed logs for debugging and system validation. The test in question is highly coupled to logging to the point that verification of behaviors is difficult without inspecting the logs directly. The value of the refactor was determined to greatly outweigh the value of this edge case test verification. Instead of removing the test entirely, I disabled it with a note for future maintainers about why it's disabled. Should we find in the future that this test case is important, the structure is there and available to whomever decides to reimplement the test.

mcook42 added 4 commits April 6, 2025 12:09
Deleted Maven wrapper files to clean up the project. These files are no longer required as the project setup does not rely on the Maven wrapper tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant