Custom Classloaders

Remi Forax forax at univ-mlv.fr
Fri Jan 26 11:27:29 UTC 2024


> From: "Robert Scholte" <rfscholte at apache.org>
> To: "jigsaw-dev" <jigsaw-dev at openjdk.org>
> Sent: Friday, January 26, 2024 11:46:51 AM
> Subject: Custom Classloaders

> Hi,

Hello Robert, 
you were miss at last Devoxx.be, 

> I’m working on a new application, using jlink to build it.

> One challenge is related to the simplelogger of SLF4J (as SLF4J-api is probably
> the most used logging api, the simplelogger implementation matches my
> requirements)

> There are 2 things that I would like to adjust:

>    1. Simplelogger uses the classloader to find the simplelogger.properties ( [
>    https://github.com/qos-ch/slf4j/blob/master/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerConfiguration.java#L107-L130
>    |
>    https://github.com/qos-ch/slf4j/blob/master/slf4j-simple/src/main/java/org/slf4j/simple/SimpleLoggerConfiguration.java#L107-L130
>     ] ).

> However, I want to externalize this file to
> ${java.home}/conf/simplelogger.properties, so users can adjust it to their
> preferences. If I could change the classloader I could control where to find
> this file.

> When bundling simplelogger with jlink the jar will end up in the AppClassloader
> due to the SimpleServiceProvider.

> The only option to avoid this, is to move this jar to a separate directory and
> when making a new ModuleLayer, and use ModuleFinder with this path.

> But still, I see no way to change the classloader for this jar.

>    1. The simplelogger.properties is the default configuration, and I want to make
>     it possible to override these values.

> The simplelogger offers the option to use SystemProperties to override values
> from simplelogger.properties.

> I want to avoid that the System.properties will be polluted with entities just
> for logging.

> As far as I know it is not possible to scope System.properties: they are global,
> available for every part of the application.

> Another idea I had was to extend the simplelogging.properties resources at
> runtime using the SequenceInputStream, and for this I also need to have control
> over the classloader, as it requires to override the
> ClassLoader.getResourceAsStream()

> It seems both challenges could be solved if could control the classloader (while
> still being protected by jdk.internal.loader.Loader).

> Am I missing some feature or it there another way to solve these issues?

yes, 
as you said yourself, you box yourself in a corner by wanting to use Simplelogger which does not provide a great deal of ways to configure it. 
I'm sure they are other loggers that, while heavier, are more capable in term of configuration. 

You are cursed by your own knowledge in this case, as an application developer, you should pretend that ClassLoaders do not exist, like the rest of the application developers do. 

Let me try a trick, those are not the classloaders you are looking for :) 

> Best,

> Robert

regards, 
Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jigsaw-dev/attachments/20240126/2d62afe9/attachment.htm>


More information about the jigsaw-dev mailing list