logging-log4j2 icon indicating copy to clipboard operation
logging-log4j2 copied to clipboard

Do not require java.desktop

Open jakub-id opened this issue 1 year ago • 19 comments

When using log4j2 2.25.0 in a backend vert.x application running on a custom JRE created via jlink it's necessary to include the java.desktop module otherwise the following exception is thrown during log4j initialization:

Exception in thread "main" java.lang.NoClassDefFoundError: java/beans/PropertyChangeEvent

Please consider making dependency on java.beans (part of java.desktop) optional or allow exclusion via maven. java.desktop is the second biggest standard module after java.base and it should not be required to include it in backend applications.

jakub-id avatar Aug 30 '25 14:08 jakub-id

Hi @jakub-id,

Thanks for raising this. We’re aware of the dependency on PropertyChangeEvent. Since this class is part of the public API of LoggerContext, we couldn’t remove it in the 2.x line without introducing breaking changes.

That’s why the dependency was removed in version 3.x, which now relies only on java.base (with an optional dependency on java.xml). We’re currently evaluating whether the 3.x line is ready for a stable 3.0.0 release, so it would be very helpful if you could try 3.0.0-beta3 and share your feedback. Most of the changes are related to the modularization of the logging implementation: see our upgrade instructions for details.

For context: PropertyChangeEvent was introduced into Log4j long before Java 9 and the module system were released.

ppkarwasz avatar Aug 31 '25 08:08 ppkarwasz

Hi @ppkarwasz,

Thanks, looking forward to 3.0 then. I gave it a shot with our application but vert.x doesn't like it:

 Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.NoClassDefFoundError: Could not initialize class io.vertx.core.logging.LoggerFactory [in thread "main"]
                at io.vertx.core.impl.logging.LoggerFactory.getLogger(LoggerFactory.java:26)
                at io.vertx.core.impl.AddressResolver.<clinit>(AddressResolver.java:40)
                at io.vertx.core.dns.AddressResolverOptions.<clinit>(AddressResolverOptions.java:94)
                at io.vertx.core.VertxOptions.<init>(VertxOptions.java:144)
                at io.vertx.core.Vertx.vertx(Vertx.java:141)

This is vert.x 4.5.20, we're going to migrate to 5.x in the coming days so I'll give it another try.

jakub-id avatar Aug 31 '25 09:08 jakub-id

Hi @jakub-id,

Can you check why the initialization of io.vertx.core.logging.LoggerFactory fails? If it is due to some problem in the initialization of o.a.l.log4j.LogManager, we are happy to fix it.

ppkarwasz avatar Aug 31 '25 20:08 ppkarwasz

@ppkarwasz @jakub-id happy to improve vertx to support log4j3, if you could provide more details about the issue so we can determine what is the best option to support this

vietj avatar Sep 01 '25 07:09 vietj

@ppkarwasz @vietj as mentioned on https://github.com/eclipse-vertx/vert.x/issues/5689 the problem seems to be a missing release artifact for log4j-slf4j-impl 3.0.0-beta-3 release.

jakub-id avatar Sep 01 '25 12:09 jakub-id

@jakub-id, starting with version 2.24.0, Log4j API is embraced as the target logging API for both Log4j 2 and Log4j 3. Hence, Log4j 3 does not contain a Log4j API module, but instead implements "the" Log4j API developed in Log4j 2 branch. This ensures Log4j 2 users can safely migrate to Log4j 3 without any Log4j API compatibility concerns. (See Log4j 3 API docs.) As a result, Log4j 3 does not ship a separate log4j-slf4j-impl, which bridges SLF4J calls to Log4j API, and you can safely use log4j-slf4j-impl-2.x in Log4j 3. But we strongly recommend our users to, instead of explicitly providing each artifact version, use log4j-bom (see Log4j 3 BOM docs) and leverage the dependencies managed by it.

@jakub-id, would you mind retrying your experiment after importing log4j-bom and removing explicit version numbers, please?

vy avatar Sep 01 '25 14:09 vy

@vy Thanks, we already use the BOM: https://github.com/indexdata/reservoir/blob/0a532333a8c5a1fdd91eaf9136eb64d69a39a193/pom.xml#L50 and we don't use explicit version in the dependency section. As, mentioned in the vert.x ticket 3.0.0-beta2 works fine but 3.0.0-beta3 fails. I am not ruling out that we have something wrong in our POM but there's clearly a difference between beta2 and beta3.

jakub-id avatar Sep 01 '25 16:09 jakub-id

@jakub-id, I'm confused. You stated

the problem seems to be a missing release artifact for log4j-slf4j-impl 3.0.0-beta-3 release. ... we already use the [Log4j] BOM:

log4j-bom:3.x.x provides log4j-slf4j-impl:2.x.x. So where does the log4j-slf4j-impl:3.0.0-beta-3 dependency come from? Would you mind sharing the mvn -Dverbose dependency:tree -pl :problematicModule output, please? (An explanation of how to reproduce the issue will also be helpful.)

vy avatar Sep 01 '25 16:09 vy

@vy output of mvn -Dverbose dependency:tree -pl server attached for three different versions of log4j2 bom. beta3 is the only one that pulls log4j-slf4j-impl:2.24.1.

deps-log4j2-2_25_1.log

deps-log4j2-3.0.0-beta2.log

deps-log4j2-3.0.0-beta3.log

If you can you checkout https://github.com/indexdata/reservoir and run mvn package -Dlog4j2.version=3.0.0-beta3 it will reproduce the problem in verrt.x.

jakub-id avatar Sep 01 '25 18:09 jakub-id

@vy it's log4j-bom that pulls those versions, see:

https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-bom/3.0.0-beta2 -> log4j-slf4j2-impl:3.0.0-beta2 https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-bom/3.0.0-beta3 -> log4j-slf4j2-impl:2.24.1 (fails in vert.x)

jakub-id avatar Sep 01 '25 18:09 jakub-id

@vy it's log4j-bom that pulls those versions, see:

https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-bom/3.0.0-beta2 -> log4j-slf4j2-impl:3.0.0-beta2 https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-bom/3.0.0-beta3 -> log4j-slf4j2-impl:2.24.1 (fails in vert.x)

@jakub-id, thanks for confirming that the latest log4j-bom:3.x.x indeed pulls in log4j-slf4j[2]-impl:2.x.x dependencies as I stated. I will try to reproduce the Log4j 3 and Vert.x problem on indexdata/reservoir using the command you shared.

vy avatar Sep 01 '25 19:09 vy

@jakub-id, org.folio.okapi:okapi-core incorrectly drags in log4j-slf4j2-impl, excluding that in server/pom.xml helped with ClusterBuilderTest. There are still some other tests failing, but it has been a long night and I will try to tackle 'em later.

vy avatar Sep 01 '25 20:09 vy

@vy Thanks for spotting this, it's only the test scope but that's of course exactly what made the tests fail. I've pushed the change and it's passing https://github.com/indexdata/reservoir/pull/39/files. I'm still curious: why are there no 3.0.0-beta3 artifacts for log4j-slf4j and the BOM points back to 2.24.1?

jakub-id avatar Sep 02 '25 06:09 jakub-id

X-to-Log4j-API bridges such as log4j-jul, log4j-jcl, and log4j-slf4j2-impl don’t depend on a particular Log4j API implementation. They only depend on the API, so we release them alongside log4j-api.

That effectively gives us two parallel versioning schemes:

  • log4j-api and the bridges are currently effectively frozen at 2.24.x. For historical reasons, we’ve kept the log4j-api and log4j-core 2.x versions in sync, but note that log4j-api-2.25.x doesn’t introduce any API changes, for either users or implementors. In fact, I’m deliberately avoiding adding new helper methods so that all Log4j API implementations remain compatible with log4j-api-2.24.x. In the past, new helpers sometimes caused implementations to break when used with older API versions.
  • log4j-core and its extensions are being released on two tracks: the 2.x line and the new 3.x line.

Unlike SLF4J/Logback, we didn’t establish a separate artifact name for the API vs. the implementation early on. That means we’re “stuck” with the log4j-api / log4j-core naming, but the underlying principle remains the same.

ppkarwasz avatar Sep 02 '25 07:09 ppkarwasz

I'm still curious: why are there no 3.0.0-beta3 artifacts for log4j-slf4j and the BOM points back to 2.24.1?

<ai-assistance>

Why does Log4j 3 target the old Log4j 2 API?

  • Drop-in replacement – No code changes, just swap the Log4j Core dependency.
  • Smooth migration – Avoids the pain of introducing a new API.
  • Ecosystem stability – Prevents breaking frameworks and libraries.
  • Backend focus – Log4j 3 improves internals while keeping the API.

In short: Log4j 3 = new core, same API — upgrades without disruption.

</ai-assistance>

Let me know if this does not answer your question.

@jakub-id, I think, I've made some progress. Would you mind implementing the following changes and reporting back the result, please?

  1. In /pom.xml, set the log4j2.version to 3.0.0-beta3
  2. In /util/pom.xml, replace log4j-slf4j-impl dependency with log4j-slf4j2-impl (Because you're using SLF4J 2)
  3. In /server/pom.xml, exclude org.slf4j:jcl-over-slf4j from org.apache.solr:solr-solrj, and add commons-logging:commons-logging:1.3.5:test dependency instead (See Installing JCL-to-Log4j Bridge)
  4. In /server/pom.xml, replace log4j-slf4j-impl dependency with log4j-slf4j2-impl
  5. In /server/pom.xml, exclude org.apache.logging.log4j:log4j-1.2-api from org.folio.okapi:okapi-core (I suspect a bug in log4j-1.2-api here, I will check it later.)
  6. In /client/pom.xml, replace log4j-slf4j-impl dependency with log4j-slf4j2-impl

vy avatar Sep 02 '25 07:09 vy

@vy Thanks, I cleaned up logging per your recommendation. Excluding log4j deps pulled by okapi-core fixed the problems in vert.x. I'm still not following why log4j-slf4j2-impl is reverted to 2.24.1 in the 3.0.0-beta3 BOM but that's secondary. Thanks again and looking forward to the 3.0.0 GA.

jakub-id avatar Sep 02 '25 19:09 jakub-id

I'm still not following why log4j-slf4j2-impl is reverted to 2.24.1 in the 3.0.0-beta3 BOM but that's secondary.

3.0.0-beta2 was still delivering a log4j-api along with all bells and whistles; log4j-slf4j2-impl, log4j-to-slf4j, log4j-to-jul, etc. That is, there was 3.0.0-beta2 version of all these artifacts.

3.0.0-beta3 removed its own log4j-api, log4j-to-slf4j, etc. modules and replaced these with dependencies targeting 2.24.1 (the latest 2.x version of the date) instead. Put another way, 3.0.0-beta3 embraced the "one and only Log4j API, and that is Log4j 2 API" concept. See release notes.

Excluding log4j deps pulled by okapi-core fixed the problems in vert.x

I'm reading this as the issue is resolved. Closing the ticket. Feel free the re-open it and/or drop a comment if you have any further questions/concerns.

vy avatar Sep 03 '25 06:09 vy

@vy Thanks, the original issue has been indeed resolved but we have another problem now. One of the dependencies (okapi-core) is using a contextual logger (log4j2 config attached), which on vert.x is not a standard MDC. With beta3 that config fails with:

java.lang.IllegalArgumentException
        at io.vertx.core.impl.ContextBase.putLocal(ContextBase.java:67)
        at io.vertx.core.Context.putLocal(Context.java:289)
        at org.folio.okapi.common.logging.FolioLoggingContext.put(FolioLoggingContext.java:107)
        at org.folio.tlib.RouterCreator.lambda$mountAll$0(RouterCreator.java:44)

this is likely related to a change that removed some defensive code from okapi-core: https://github.com/folio-org/okapi/commit/f1e17983e8b18f932bba86378ad6af9c636bdf8d

Has anything changed wrt handling context variables in 3.x? I skimmed the migration guide but didn't register anything. Any pointers would be appreciated before we get down the rabbit hole again. I'll be happy to create a separate ticket if that's preferred. Thanks in advance.

log4j2.properties: https://github.com/folio-org/okapi/blob/21a46223f18490c0e16824f2ab7301212a55332c/okapi-core/src/main/resources/log4j2.properties

jakub-id avatar Sep 04 '25 09:09 jakub-id

@jakub-id, would you mind creating a new Discussion for this particular question, please?

vy avatar Sep 04 '25 11:09 vy