Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> diagnostics = generateDescriptor(sourceDir, null, null, outputDir);
void whenNoGroupIdAndArtifactId_thenWarningIsEmittedWhenConfigured(
@TempDir(cleanup = CleanupMode.NEVER) Path outputDir) throws Exception {
List<String> 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))
Expand All @@ -241,9 +242,13 @@ void whenNoGroupIdAndArtifactId_thenWarningIsPrinted(@TempDir(cleanup = CleanupM
}

@Test
void noteEmittedByDefaultWithLog4jPrefix(@TempDir Path outputDir) throws Exception {
List<Diagnostic<? extends JavaFileObject>> diagnostics =
generateDiagnostics(sourceDir, GROUP_ID, ARTIFACT_ID, outputDir);
void noteEmittedWhenConfiguredWithLog4jPrefix(@TempDir Path outputDir) throws Exception {
List<Diagnostic<? extends JavaFileObject>> 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
Expand All @@ -253,13 +258,9 @@ void noteEmittedByDefaultWithLog4jPrefix(@TempDir Path outputDir) throws Excepti
}

@Test
void notesSuppressedWithoutAffectingMetadataGeneration(@TempDir Path outputDir) throws Exception {
List<Diagnostic<? extends JavaFileObject>> diagnostics = generateDiagnostics(
sourceDir,
GROUP_ID,
ARTIFACT_ID,
outputDir,
"-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=warning");
void notesSuppressedByDefaultWithoutAffectingMetadataGeneration(@TempDir Path outputDir) throws Exception {
List<Diagnostic<? extends JavaFileObject>> diagnostics =
generateDiagnostics(sourceDir, GROUP_ID, ARTIFACT_ID, outputDir);

assertThat(diagnostics)
.noneMatch(diagnostic -> diagnostic.getKind() == Diagnostic.Kind.NOTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ void ignoreWarningWhenSuppressWarningsIsPresent() {
}

@Test
void noteEmittedByDefault() {
void noteEmittedWhenConfigured() {
setupWithOptions("-A" + PluginProcessor.MIN_ALLOWED_MESSAGE_KIND_OPTION + "=NOTE");
final List<Diagnostic<? extends JavaFileObject>> noteDiagnostics = diagnosticCollector.getDiagnostics().stream()
.filter(d -> d.getKind() == Diagnostic.Kind.NOTE)
.collect(Collectors.toList());
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public class GraalVmProcessor extends AbstractProcessor {
private final Map<String, ReachabilityMetadata.Type> reachableTypes = new HashMap<>();
private final List<Element> 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) {
Expand All @@ -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())));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class PluginProcessor extends AbstractProcessor {
* </p>
* <p>
* 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}.
* </p>
*/
static final String MIN_ALLOWED_MESSAGE_KIND_OPTION = "log4j.plugin.processor.minAllowedMessageKind";
Expand All @@ -97,7 +97,8 @@ public class PluginProcessor extends AbstractProcessor {

private final List<Element> 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) {
Expand All @@ -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())));
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/changelog/.2.x.x/4225_plugin_processor_allowed_messages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="4225" link="https://github.com/apache/logging-log4j2/issues/4225"/>
<issue id="4256" link="https://github.com/apache/logging-log4j2/pull/4256"/>
<description format="asciidoc">
Switch the default minimum allowed plugin processor message severity from NOTE to ERROR
</description>
</entry>
2 changes: 1 addition & 1 deletion src/changelog/.2.x.x/4225_plugin_processor_messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<issue id="4225" link="https://github.com/apache/logging-log4j2/issues/4225"/>
<issue id="4228" link="https://github.com/apache/logging-log4j2/pull/4228"/>
<description format="asciidoc">
Expand Down
13 changes: 6 additions & 7 deletions src/site/antora/modules/ROOT/pages/manual/plugins.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Title still says suppressing, but this now turns notes on

Suggested change
.Suppressing annotation processor notes in strict build environments
.Enabling annotation processor notes

[%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]
=====
Expand All @@ -232,7 +231,7 @@ Maven::
[source,xml]
----
<compilerArgs>
<arg>-Alog4j.plugin.processor.minAllowedMessageKind=WARNING</arg>
<arg>-Alog4j.plugin.processor.minAllowedMessageKind=NOTE</arg>
</compilerArgs>
----

Expand All @@ -241,7 +240,7 @@ Gradle::
[source,groovy]
----
compileJava {
options.compilerArgs << '-Alog4j.plugin.processor.minAllowedMessageKind=WARNING'
options.compilerArgs << '-Alog4j.plugin.processor.minAllowedMessageKind=NOTE'
}
----
=====
Expand Down
Loading