apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.35k stars 1.59k forks source link

Classes should no longer be specified via system properties #1977

Open jvz opened 10 months ago

jvz commented 10 months ago

Over time, we collected many places in the codebase that accept an override by specifying a class name through a system property. These typically worked by guessing at an appropriate ClassLoader to use to load the class and then using reflection to instantiate it. This pattern is problematic in module systems like the Java module system and OSGi due to stronger encapsulation guarantees. As we've already implemented for plugins, anything else remaining that customizes class selection based on system properties should be updated to use java.util.ServiceLoader (when customizing in log4j-api) or specified in bundle classes installed via a service loader (which is also where some of the old system properties customizations are currently supported and can be simplified).

In general, the goal of this is to eliminate the use of LoaderUtil::load and any other implicit ClassLoader usage.

For some concrete examples, as I've worked on this, I've found the following interfaces that can be specified by system properties which are generally being refactored into what I said above.

Log4j API

The following interfaces are being loaded through service loader provider classes. These are similar in a way to the previous iteration of the Provider service class which grouped some of these things together.

There are some other service loader classes in the API, but they're either new or were already using service loading.

Log4j Core

The following interfaces either allow for overriding a default using a system property or are only configured through a system property. These should be updated to use bindings (or service loaders if that's not possible).

Other

There are a few standalone modules that use LoaderUtil::loadClass for things that can be ported to use DI directly. There are some other places still using LoaderUtil or Loader that should be cleaned up alongside all this.

jvz commented 9 months ago

So far, I've managed to migrate most of these things. I had to disable some tests for now, though, as they significantly abuse internal details of the legacy way of doing things in order to work.

jvz commented 9 months ago

Although some of them might work properly after I try to figure out yet another architecture for the JUnit 5 extensions since splitting them up into multiple annotations doesn't seem to work too well for some reason.

jvz commented 8 months ago

Extracted https://github.com/apache/logging-log4j2/issues/2204 from work done in previous attempt at this issue.