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

Load pre-loaded lookups via SPI, rather than hard-code in Interpolator.

Open tbwork opened this issue 5 years ago • 14 comments

issue link: Provide a way to add customized StrLookUps in Interpolator initialization.

tbwork avatar Aug 10 '20 06:08 tbwork

What's wrong with the CI? The change should have nothing to do with the error. :)

tbwork avatar Aug 10 '20 09:08 tbwork

No worries about the Travis check. The GitHub Actions checks are the important ones. We need to disable our old Travis checks.

jvz avatar Aug 10 '20 13:08 jvz

The only concern I have with this is the overhead of ServiceLoader during startup. I recently had to make ServiceLoader run asynchronously during startup while loading Custom ThreadContextDataProviders. This will likely cause the same problem. We should make that more generic so that components such as them can register their service for asynchronous initialization. But that would be done in a different PR.

rgoers avatar Aug 10 '20 15:08 rgoers

@jvz I dislike ServiceLookup as it is describing its implementation method, not its purpose, which is to allow a way for users to hook into a Lookup that is configured to run before any standard plugins have been processed.

Perhaps that is an indication that the mechanism provided here is incorrect. Perhaps the service loader should be looking for Lookup Plugins that are also declared as services. We could replace the way all these lookups are loaded if we do that.

rgoers avatar Aug 10 '20 16:08 rgoers

@rgoers Agree! This PR is intended to do that as you proposed. Hope this could be supported ASAP :) . Have a nice day.

tbwork avatar Aug 10 '20 17:08 tbwork

@rgoers Do you mean all the pre-loaded Lookups should be loaded via ServiceLoader? I am glad to contribute to this part if you do not mind. :)

tbwork avatar Aug 10 '20 17:08 tbwork

Yes, that is exactly what I mean. Feel free to modify this PR to do that. In essence, StrLookup would become the service interface and every plugin to be pre-loaded would be listed in the services file instead of hard coded in Interpolator.

rgoers avatar Aug 10 '20 18:08 rgoers

@rgoers ok.

tbwork avatar Aug 11 '20 02:08 tbwork

@jvz @rgoers It seems the Log4jPlugins class is moved from org.apache.logging.log4j.core.plugins. However in Activator class, Log4jPlugins is still imported as "org.apache.logging.log4j.core.plugins.Log4jPlugins".

tbwork avatar Aug 11 '20 07:08 tbwork

@rgoers @jvz Hi, are you busy? Could you get round to take a review and merge :).

tbwork avatar Aug 14 '20 04:08 tbwork

These changes are not what I expected. All the Lookups should remain plugins. You should have only had to modify Interpolator to load the required lookups via ServiceLoader and then added the Lookups you wanted to load to the file used by ServiceLoader. The Lookups would be loaded again as normal as Plugins during configuration.

rgoers avatar Aug 15 '20 23:08 rgoers

@rgoers I propably misunderstood your opinion:

In essence, StrLookup would become the service interface and every plugin to be pre-loaded would be listed in the services file instead of hard coded in Interpolator.

I guess following is what you want:

  1. Load all lookups as plugins via SPI.
  2. Load all lookup plugins via the plugin mechanism.

Is it right ?

tbwork avatar Aug 17 '20 03:08 tbwork

@rgoers Hi, rgoers. After debuging and reading the log4j2 codes, I realize that the public Interpolator(final Map<String, String> properties) is called several times during factory creation stage. Is that reasonable to create so many StrLookup instances with the same type?

tbwork avatar Aug 17 '20 06:08 tbwork

Well, the Interpolator constructor that has the hard-coded list of lookups is initializing the lookups that could be using while constructing the configuration. The other lookups would be used at runtime. There is no reason that I can think of that all the Lookups couldn't be initialized once via ServiceLoader except that it would break compatibility with users who have created their own Lookups as plugins. We could possibly load the serviceloader Lookups once and then add the ones defined as plugins later.

rgoers avatar Aug 17 '20 16:08 rgoers

This was closed automatically by Github because we renamed the release-2.x branch to 2.x. Feel free to resubmit to the 2.x branch.

ppkarwasz avatar Mar 01 '23 07:03 ppkarwasz