Skip to content

Commit

Permalink
Support configure log level for recipe changes (#852)
Browse files Browse the repository at this point in the history
* Support configure log level for recipe changes

* Drop `MavenLog.logTo` to simplify API

* Use `protected void log(CharSequence)`

* Make log level argument explicit, as suggested

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
runeflobakk and timtebeek authored Sep 2, 2024
1 parent 2fa37f0 commit 6b2bdf3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ public abstract class AbstractRewriteBaseRunMojo extends AbstractRewriteMojo {
@Nullable
protected LinkedHashSet<String> options;

/**
* The level used to log changes performed by recipes.
*/
@Parameter(property = "rewrite.recipeChangeLogLevel", defaultValue = "WARN")
protected LogLevel recipeChangeLogLevel;

protected void log(LogLevel logLevel, CharSequence content) {
switch (logLevel) {
case DEBUG:
getLog().debug(content);
break;
case INFO:
getLog().info(content);
break;
case WARN:
getLog().warn(content);
break;
case ERROR:
getLog().error(content);
break;
}
}

/**
* Attempt to determine the root of the git repository for the given project.
* Many Gradle builds co-locate the build root with the git repository root, but that is not required.
Expand Down Expand Up @@ -438,7 +461,7 @@ private void logRecipe(RecipeDescriptor rd, String prefix) {
recipeString.append(": {").append(opts).append("}");
}
}
getLog().warn(recipeString.toString());
log(recipeChangeLogLevel, recipeString.toString());
for (RecipeDescriptor rchild : rd.getRecipeList()) {
logRecipe(rchild, prefix + " ");
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,40 @@ public void execute() throws MojoExecutionException {
Duration estimateTimeSaved = Duration.ZERO;
for (Result result : results.generated) {
assert result.getAfter() != null;
getLog().warn("Generated new file " +
result.getAfter().getSourcePath().normalize() +
" by:");
log(recipeChangeLogLevel, "Generated new file " +
result.getAfter().getSourcePath().normalize() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.deleted) {
assert result.getBefore() != null;
getLog().warn("Deleted file " +
result.getBefore().getSourcePath().normalize() +
" by:");
log(recipeChangeLogLevel, "Deleted file " +
result.getBefore().getSourcePath().normalize() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.moved) {
assert result.getAfter() != null;
assert result.getBefore() != null;
getLog().warn("File has been moved from " +
result.getBefore().getSourcePath().normalize() + " to " +
result.getAfter().getSourcePath().normalize() + " by:");
log(recipeChangeLogLevel, "File has been moved from " +
result.getBefore().getSourcePath().normalize() + " to " +
result.getAfter().getSourcePath().normalize() + " by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}
for (Result result : results.refactoredInPlace) {
assert result.getBefore() != null;
getLog().warn("Changes have been made to " +
result.getBefore().getSourcePath().normalize() +
" by:");
log(recipeChangeLogLevel, "Changes have been made to " +
result.getBefore().getSourcePath().normalize() +
" by:");
logRecipesThatMadeChanges(result);
estimateTimeSaved = estimateTimeSavedSum(result, estimateTimeSaved);
}

getLog().warn("Please review and commit the results.");
getLog().warn("Estimate time saved: " + formatDuration(estimateTimeSaved));
log(recipeChangeLogLevel, "Please review and commit the results.");
log(recipeChangeLogLevel, "Estimate time saved: " + formatDuration(estimateTimeSaved));

try {
for (Result result : results.generated) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/openrewrite/maven/LogLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.openrewrite.maven;

public enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR
}

0 comments on commit 6b2bdf3

Please sign in to comment.