diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessorTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessorTest.java index f46150f4a35..eb14c49aa97 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessorTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessorTest.java @@ -216,9 +216,10 @@ void reachabilityMetadataPath(@Nullable String groupId, @Nullable String artifac } @Test - void whenNoGroupIdAndArtifactId_thenWarningIsPrinted(@TempDir(cleanup = CleanupMode.NEVER) Path outputDir) - throws Exception { - List diagnostics = generateDescriptor(sourceDir, null, null, outputDir); + void whenNoGroupIdAndArtifactId_thenWarningIsEmittedWhenConfigured( + @TempDir(cleanup = CleanupMode.NEVER) Path outputDir) throws Exception { + List diagnostics = generateDescriptor( + sourceDir, null, null, outputDir, "-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=WARNING"); assertThat(diagnostics).hasSize(1); // The warning message should contain the information about the missing groupId and artifactId arguments assertThat(diagnostics.get(0)) @@ -241,9 +242,13 @@ void whenNoGroupIdAndArtifactId_thenWarningIsPrinted(@TempDir(cleanup = CleanupM } @Test - void noteEmittedByDefaultWithLog4jPrefix(@TempDir Path outputDir) throws Exception { - List> diagnostics = - generateDiagnostics(sourceDir, GROUP_ID, ARTIFACT_ID, outputDir); + void noteEmittedWhenConfiguredWithLog4jPrefix(@TempDir Path outputDir) throws Exception { + List> diagnostics = generateDiagnostics( + sourceDir, + GROUP_ID, + ARTIFACT_ID, + outputDir, + "-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=NOTE"); assertThat(diagnostics) .anyMatch(diagnostic -> diagnostic.getKind() == Diagnostic.Kind.NOTE @@ -253,13 +258,9 @@ void noteEmittedByDefaultWithLog4jPrefix(@TempDir Path outputDir) throws Excepti } @Test - void notesSuppressedWithoutAffectingMetadataGeneration(@TempDir Path outputDir) throws Exception { - List> diagnostics = generateDiagnostics( - sourceDir, - GROUP_ID, - ARTIFACT_ID, - outputDir, - "-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=warning"); + void notesSuppressedByDefaultWithoutAffectingMetadataGeneration(@TempDir Path outputDir) throws Exception { + List> diagnostics = + generateDiagnostics(sourceDir, GROUP_ID, ARTIFACT_ID, outputDir); assertThat(diagnostics) .noneMatch(diagnostic -> diagnostic.getKind() == Diagnostic.Kind.NOTE diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorPublicSetterTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorPublicSetterTest.java index 3772cced4dc..92dacebe240 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorPublicSetterTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessorPublicSetterTest.java @@ -113,7 +113,8 @@ void ignoreWarningWhenSuppressWarningsIsPresent() { } @Test - void noteEmittedByDefault() { + void noteEmittedWhenConfigured() { + setupWithOptions("-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=NOTE"); final List> noteDiagnostics = diagnosticCollector.getDiagnostics().stream() .filter(d -> d.getKind() == Diagnostic.Kind.NOTE) .collect(Collectors.toList()); @@ -141,7 +142,7 @@ void errorsStillEmittedWhenMinKindIsError() { @ParameterizedTest @ValueSource(strings = {"NOTE", "note"}) - void explicitNoteKindBehavesLikeDefault(final String kindValue) { + void explicitNoteKindEmitsNotes(final String kindValue) { setupWithOptions("-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=" + kindValue); assertThat(errorDiagnostics).anyMatch(d -> d.getMessage(Locale.ROOT) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessor.java index 05528f96a92..986a13d20ce 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/GraalVmProcessor.java @@ -84,7 +84,8 @@ public class GraalVmProcessor extends AbstractProcessor { private final Map reachableTypes = new HashMap<>(); private final List processedElements = new ArrayList<>(); private Annotations annotationUtil; - private Diagnostic.Kind minAllowedMessageKind = Diagnostic.Kind.NOTE; + private static final Diagnostic.Kind DEFAULT_MIN_ALLOWED_MESSAGE_KIND = Diagnostic.Kind.ERROR; + private Diagnostic.Kind minAllowedMessageKind = DEFAULT_MIN_ALLOWED_MESSAGE_KIND; @Override public synchronized void init(ProcessingEnvironment processingEnv) { @@ -95,15 +96,21 @@ public synchronized void init(ProcessingEnvironment processingEnv) { try { minAllowedMessageKind = Diagnostic.Kind.valueOf(kindValue.toUpperCase(Locale.ROOT)); } catch (final IllegalArgumentException e) { - printMessage( - Diagnostic.Kind.WARNING, - String.format( - "%s: unrecognized value `%s` for option `%s`, using default `%s`. Valid values: %s", - GraalVmProcessor.class.getName(), - kindValue, - PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION, - Diagnostic.Kind.NOTE, - Arrays.toString(Diagnostic.Kind.values()))); + // We should not use `GraalVmProcessor::printMessage`, since we + // report a failure on the user-provided `Diagnostic.Kind` that + // `GraalVmProcessor::printMessage` depends on. + processingEnv + .getMessager() + .printMessage( + Diagnostic.Kind.WARNING, + String.format( + "%s%s: unrecognized value `%s` for option `%s`, using default `%s`. Valid values: %s", + MESSAGE_PREFIX, + GraalVmProcessor.class.getName(), + kindValue, + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION, + DEFAULT_MIN_ALLOWED_MESSAGE_KIND, + Arrays.toString(Diagnostic.Kind.values()))); } } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java index 51b7e091090..8455d66dd5f 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/processor/PluginProcessor.java @@ -83,7 +83,7 @@ public class PluginProcessor extends AbstractProcessor { *

*

* Accepted values (case-insensitive): {@code NOTE}, {@code WARNING}, {@code MANDATORY_WARNING}, - * {@code ERROR}, {@code OTHER}. Defaults to {@code NOTE}. + * {@code ERROR}, {@code OTHER}. Defaults to {@code ERROR}. *

*/ static final String MIN_ALLOWED_MESSAGE_KIND_OPTION = "log4j.plugin.processor.minAllowedMessageKind"; @@ -97,7 +97,8 @@ public class PluginProcessor extends AbstractProcessor { private final List processedElements = new ArrayList<>(); private final PluginCache pluginCache = new PluginCache(); - private Diagnostic.Kind minAllowedMessageKind = Diagnostic.Kind.NOTE; + private static final Diagnostic.Kind DEFAULT_MIN_ALLOWED_MESSAGE_KIND = Diagnostic.Kind.ERROR; + private Diagnostic.Kind minAllowedMessageKind = DEFAULT_MIN_ALLOWED_MESSAGE_KIND; @Override public void init(final ProcessingEnvironment processingEnv) { @@ -107,15 +108,21 @@ public void init(final ProcessingEnvironment processingEnv) { try { minAllowedMessageKind = Diagnostic.Kind.valueOf(kindValue.toUpperCase(Locale.ROOT)); } catch (final IllegalArgumentException e) { - printMessage( - Diagnostic.Kind.WARNING, - String.format( - "%s: unrecognized value `%s` for option `%s`, using default `%s`. Valid values: %s", - PluginProcessor.class.getName(), - kindValue, - MIN_ALLOWED_MESSAGE_KIND_OPTION, - Diagnostic.Kind.NOTE, - Arrays.toString(Diagnostic.Kind.values()))); + // We should not use `PluginProcessor::printMessage`, since we + // report a failure on the user-provided `Diagnostic.Kind` that + // `PluginProcessor::printMessage` depends on. + processingEnv + .getMessager() + .printMessage( + Diagnostic.Kind.WARNING, + String.format( + "%s%s: unrecognized value `%s` for option `%s`, using default `%s`. Valid values: %s", + MESSAGE_PREFIX, + PluginProcessor.class.getName(), + kindValue, + MIN_ALLOWED_MESSAGE_KIND_OPTION, + DEFAULT_MIN_ALLOWED_MESSAGE_KIND, + Arrays.toString(Diagnostic.Kind.values()))); } } } diff --git a/src/changelog/.2.x.x/4225_plugin_processor_allowed_messages.xml b/src/changelog/.2.x.x/4225_plugin_processor_allowed_messages.xml new file mode 100644 index 00000000000..0427c07389e --- /dev/null +++ b/src/changelog/.2.x.x/4225_plugin_processor_allowed_messages.xml @@ -0,0 +1,13 @@ + + + + + + Switch the default minimum allowed plugin processor message severity from NOTE to ERROR + + diff --git a/src/changelog/.2.x.x/4225_plugin_processor_messages.xml b/src/changelog/.2.x.x/4225_plugin_processor_messages.xml index f52ac661a7e..9a8eb33686f 100644 --- a/src/changelog/.2.x.x/4225_plugin_processor_messages.xml +++ b/src/changelog/.2.x.x/4225_plugin_processor_messages.xml @@ -4,7 +4,7 @@ xsi:schemaLocation=" https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" - type="changed"> + type="fixed"> diff --git a/src/site/antora/modules/ROOT/pages/manual/plugins.adoc b/src/site/antora/modules/ROOT/pages/manual/plugins.adoc index a647ef5024a..3f5fe347c0f 100644 --- a/src/site/antora/modules/ROOT/pages/manual/plugins.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/plugins.adoc @@ -218,12 +218,11 @@ Provide these values to the processor using the `log4j.graalvm.groupId` and `log .Suppressing annotation processor notes in strict build environments [%collapsible] ==== -Some build environments treat all compiler notes or warnings as errors (e.g., Maven with `-Werror` or Gradle with `options.compilerArgs << '-Werror'`). -By default, `PluginProcessor` and `GraalVmProcessor` emit `NOTE`-level diagnostics when they write their descriptors, which can cause the build to fail in those environments. -To suppress these informational notes, pass the `log4j.plugin.processor.minAllowedMessageKind` annotation processor option with a value of `WARNING` or `ERROR`. -This instructs aforementioned processors to only emit diagnostics at or above the specified severity, silencing routine notes while preserving genuine warnings and errors. +`PluginProcessor` and `GraalVmProcessor` emit `NOTE`-level diagnostics when they write their descriptors. +To enable these informational notes, pass the `log4j.plugin.processor.minAllowedMessageKind` annotation processor option with a value of less severity than `ERROR`, which is the default. +This instructs aforementioned processors to only emit diagnostics at or above the specified severity. -Accepted values (case-insensitive): `NOTE` (default), `WARNING`, `MANDATORY_WARNING`, `ERROR`, `OTHER`. +Accepted values (case-insensitive): `NOTE`, `WARNING`, `MANDATORY_WARNING`, `ERROR` (default), `OTHER`. [tabs] ===== @@ -232,7 +231,7 @@ Maven:: [source,xml] ---- - -Alog4j.plugin.processor.minAllowedMessageKind=WARNING + -Alog4j.plugin.processor.minAllowedMessageKind=NOTE ---- @@ -241,7 +240,7 @@ Gradle:: [source,groovy] ---- compileJava { - options.compilerArgs << '-Alog4j.plugin.processor.minAllowedMessageKind=WARNING' + options.compilerArgs << '-Alog4j.plugin.processor.minAllowedMessageKind=NOTE' } ---- =====