Skip to content
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

Support configure log level for recipe changes #852

Merged
merged 5 commits into from
Sep 2, 2024

Conversation

runeflobakk
Copy link
Contributor

@runeflobakk runeflobakk commented Aug 31, 2024

What's changed?

Adding configurable log level for when running recipe(s) result in changes:

<configuration>
    <recipeChangeLogLevel>{DEBUG|INFO|WARN|ERROR}</recipeChangeLogLevel>

The default is "WARN" to align with existing behaviour.
It only affects logging from recipe results (which are currently hardcoded to .warn(..)). Any other logging is intentionally not affected by this property.

What's your motivation?

I, and I see indications from others as well, use OpenRewrite to always rewrite existing code from some source. I.e. the rewriting and changes are expected and handled by further steps in a build. For these use cases it is not appropriate to log performed changes as warnings. The motivation is to keep a clean build log, and only issue warnings for actual issues.

Example use-cases I believe are relevant for this change:

Anything in particular you'd like reviewers to focus on?

The name of the configuration property. I currently named is as recipeChangeLogLevel, with the intention to communicate that it controls the log level used for logging changes performed by recipes. I am very open for any better name, if anyone has an idea for it 😅

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

I am one of those people coding in Eclipse, but I believe the small changes introduced here should not be crashing with how IntelliJ would format the source files.

@runeflobakk runeflobakk marked this pull request as ready for review August 31, 2024 23:01
@timtebeek timtebeek self-requested a review September 2, 2024 09:18
@timtebeek timtebeek added the enhancement New feature or request label Sep 2, 2024
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Great to see! Indeed seems like a valid use case; I've minimized the changes & API by folding log under AbstractRewriteBaseRunMojo, as opposed to a separate class that requires repeated arguments.

@Parameter(property = "rewrite.recipeChangeLogLevel", defaultValue = "WARN")
protected LogLevel recipeChangeLogLevel;

protected void log(CharSequence content) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All fine with this change. Just wanted to explain why I implemented it the way I originally did.

Now you essentially have a method in an abstract class, available in all the different implementations, and it only communicates "log" and a string to be logged. It is very implicit that the actual log level is affected by a LogLevel field with very specific semantics. I didn't want to introduce a method named only "log", which is intended for a very specific use: to log something that changed due to a rewrite.

To me, it seems better to have a method which accepts an explicit log level (e.g. the configured recipeChangeLogLevel), because the knowledge about what is logged is at the site where one invokes the logging-method. Now one really has to know that the method named only "log" is specifically for logging changes made from a recipe, instead of the method accepting the loglevel as a parameter and carrying out as instructed, and the knowledge about why a certain loglevel is used is kept at the call site.

This can be implemented as a method accepting the LogLevel in the abstract class, absolutely, and that would also align with my original "design". Externalizing it to a separate (package scoped) class is just my preference to not tie too much concerns into abstract classes and making them available through inheritance.

Just wanted to mention it for you to consider :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A (better IMHO) alternative would be to perhaps have

protected void log(LogLevel level, CharSequence content) {

instead, as it would not tie the log-method implicitly to the recipeChangeLogLevel. And you avoid the separate class.

It would be very easy for other code to evolve to invoke the new log-method accepting only the message, and not logging anything related to "changes from rewriting", but gets the log level unintentionally configured from recipeChangeLogLevel.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you prefer not repeating (passing) recipeChangeLogLevel from the different logging sites, I would rename the logging-method specifically for its intended use, perhaps logRecipeChange(CharSequence) or something, to communicate its intended use. It is not a general log method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for that context! I've adopted your proposal of log(LogLevel, CharSequence)

Copy link
Contributor Author

@runeflobakk runeflobakk left a comment

Choose a reason for hiding this comment

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

👍👍👍
Thanks for a very swift review! :)

@timtebeek timtebeek linked an issue Sep 2, 2024 that may be closed by this pull request
@timtebeek timtebeek merged commit 6b2bdf3 into openrewrite:main Sep 2, 2024
1 check passed
runeflobakk added a commit to digipost/digipost-xml that referenced this pull request Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Configurable log-level for changes made by recipes
2 participants