Jonathan Barbero
Jonathan Barbero

Reputation: 2564

logback configuration files per jar

I would like to deliver jar files with its own logback logging configuration. The common way to configure logback is with the default file logback.xml that logback library reads from the classpath's root (works for application servers or not).

You could include another files from the main logback config file (didn't try it), but I don't know which jars will be in the classpath and which ones require the log configuration.

Plus, the jars could be used in a command line application or application servers (shared library or not).

I thought that maybe I could get the filepath to the jar and check if there is a config file there, and try to read the configuration programmatically and load it with JoranConfigurator.

// This is the way I find to get the path to the jar
String path = ClassThatWantsALogger.class.getProtectionDomain().getCodeSource()
            .getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");// the path to the jar

But this approach could fail because depends on the security restrictions, maybe fails in Linux or in application servers. It's a hard approach for a problem that probably has a better solution.

Do you imagine a way to manage that jars could have its own logback config file that works for any environment?

Upvotes: 2

Views: 2846

Answers (1)

Michael-O
Michael-O

Reputation: 18415

I will cite myself to answer your question: Logging configuration is the concern (separation of concerns) of the client application. It's his decision to make where, how and what will be logged. You shall not impose anything on it by our library.

Upvotes: 2

Related Questions