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

Upgrading 2.21.1 to 2.22.0 causes compilation failure on BaselineIgnore #2232

Closed
big-andy-coates opened this issue Jan 23, 2024 · 5 comments
Closed

Comments

@big-andy-coates
Copy link

Description

When updating log4j from v2.2.21.1 to v2.22., I see the build fail the following error:

/home/runner/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.22.1/bea6fede6328fabafd7e68363161a7ea6605abd1/log4j-api-2.22.1.jar(/org/apache/logging/log4j/Level.class): warning: Cannot find annotation method 'value()' in type 'BaselineIgnore': class file for aQute.bnd.annotation.baseline.BaselineIgnore not found

This looks to be related to this PR and specifically this commit, which adds the @BaselineIgnore annotation to the Level class.

For whatever reason, my build doesn't seem to be pulling in the annotation as a dependency and it doesn't like it. I'm not doing anything funky, just adding a Gradle dependency:

val log4jVersion = "2.22.0"

dependencies {
    ...
    implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
    runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion")
    ...
}

Any ideas?

Configuration

Version: moving from 2.21.1 to 2.22.0

Operating system: MacOS locally and Linux GitHub Runner

JDK: adopt JDK 17

Logs

Run ./gradlew build coveralls --stacktrace
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :buildSrc:generateExternalPluginSpecBuilders
> Task :buildSrc:extractPrecompiledScriptPluginPlugins
> Task :buildSrc:compilePluginsBlocks

> Task :buildSrc:generatePrecompiledScriptPluginAccessors
Project : => no module-info.java found

> Task :buildSrc:generateScriptPluginAdapters
> Task :buildSrc:pluginDescriptors
> Task :buildSrc:processResources
> Task :buildSrc:compileKotlin
> Task :buildSrc:compileJava NO-SOURCE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:classes
> Task :buildSrc:jar
> Task :buildSrc:inspectClassesForKotlinIC

> Task :compileJava
/home/runner/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.22.1/bea6fede63[28](https://github.com/creek-service/json-schema-validation-comparison/actions/runs/7632745461/job/20793563134?pr=123#step:7:29)fabafd7e6836[31](https://github.com/creek-service/json-schema-validation-comparison/actions/runs/7632745461/job/20793563134?pr=123#step:7:32)61a7ea6605abd1/log4j-api-2.22.1.jar(/org/apache/logging/log4j/Level.class): warning: Cannot find annotation method 'value()' in type 'BaselineIgnore': class file for aQute.bnd.annotation.baseline.BaselineIgnore not found
error: warnings found and -Werror specified
/home/runner/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.22.1/bea6fede6[32](https://github.com/creek-service/json-schema-validation-comparison/actions/runs/7632745461/job/20793563134?pr=123#step:7:33)8fabafd7e68[36](https://github.com/creek-service/json-schema-validation-comparison/actions/runs/7632745461/job/20793563134?pr=123#step:7:37)3161a7ea6605abd1/log4j-api-2.22.1.jar(/org/apache/logging/log4j/Level.class): warning: Cannot find annotation method 'value()' in type 'BaselineIgnore': class file for aQute.bnd.annotation.baseline.BaselineIgnore not found
1 error
2 warnings

> Task :compileJava FAILED

Reproduction

PR reproducing the issue: creek-service/json-schema-validation-comparison#123

@vy
Copy link
Member

vy commented Jan 24, 2024

@big-andy-coates, I guess this is similar to the issue reported in #2144. AFAIU, you are using -Xlint:all,-serial,-requires-automatic,-requires-transitive-automatic,-module,-processing, hence the following warning

Cannot find annotation method 'value()' in type 'BaselineIgnore': class file for aQute.bnd.annotation.baseline.BaselineIgnore not found

in combination with -Werror terminates the compilation. Would you mind checking if adding -classFile to your -Xlint:all fixes the problem, please?

@vy
Copy link
Member

vy commented Jan 24, 2024

@big-andy-coates, a side note: Are you sure you need log4j-core at compile-time? It should ideally be a runtimeOnly dependency. If that does not work for you, I would like to know why.

@big-andy-coates
Copy link
Author

big-andy-coates commented Jan 24, 2024

@big-andy-coates, a side note: Are you sure you need log4j-core at compile-time? It should ideally be a runtimeOnly dependency. If that does not work for you, I would like to know why.

In this instance I'm interacting with Log4j to programmatically disable all logging (which I don't think is possible with only Slf4J). I'm guessing I could/should switch the compiletime dependency to log4j-api...

Actually, tried that and got compilation error and I'm using LoggerContext:

private static void disableLog4J() {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
        config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.OFF);
        ctx.updateLoggers(config);
    }

@big-andy-coates
Copy link
Author

-classFile

Adding -classfile, (not -classFile), does fix the issue. Many thanks,

@vy vy closed this as completed Jan 24, 2024
@ppkarwasz
Copy link
Contributor

In this instance I'm interacting with Log4j to programmatically disable all logging (which I don't think is possible with only Slf4J). I'm guessing I could/should switch the compiletime dependency to log4j-api...

Actually, tried that and got compilation error and I'm using LoggerContext:

private static void disableLog4J() {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
        config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(Level.OFF);
        ctx.updateLoggers(config);
    }

Logging APIs don't provide methods to configure the logging backend (and probably should not provide one).

For a basic usage, you should use the Configurator class from log4j-core

try {
    Configurator.setRootLevel(Level.OFF);
} catch (final NoClassDefFoundError | ClassCastException e) {
    // either `log4j-core` is not on the classpath or `log4j-api` is bound to another implementation (e.g. `log4j-to-slf4j`).
}

Many integrator libraries (like Spring Boot) provide an abstraction to deal with programmatic configuration of an à-priori unknown logging backend. In Spring Boot you'd write:

final ClassLoader cl = ...
LoggingSystem.get(cl).setLogLevel(LoggingSystem.ROOT_LOGGER_NAME, LogLevel.OFF);

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

No branches or pull requests

3 participants