mvd
mvd

Reputation: 2720

How do I configure log4j logging for a jar?

I have a project A with log4j.jar on its build path. I have a number of classes that have logging statements in the form of:

Logger _log = Logger.getLogger(A.<>.class);
...
_log.info("...");

I am exporting the project as a jar into another project B. Project B already has its own log4j jar and it's own .xml configuration file. I want to configure particular classes from A to log to Console Apender at varying "levels". How do I do this, please?

Upvotes: 6

Views: 5767

Answers (3)

eis
eis

Reputation: 53563

Well, basically, you shouldn't do that. Think of it this way: if that would be done that way, each library that is included in any application would host its own logging configuration, very potentially overriding any of those that are in the application, and each other, in non-specified order. You wouldn't want that. So do not.

[In case you really, really want to do it, you could have properties file in the jar that could be overriden by an xml file in the main application. See details here. But don't. :) ]

Upvotes: 1

John Watts
John Watts

Reputation: 8885

Pick a version of log4j.jar, probably the newer one. You can only use one version. You need to merge project A's and project B's logging configuration. You could do this in log4j classes at runtime, but don't. Just bite the bullet and merge them once in source control.

Upvotes: 1

theglauber
theglauber

Reputation: 29675

In general, you don't. In general, you want to have one log4j configuration file. But i suppose you could load your configuration explicitly from your code, as described in the log4j documentation

Upvotes: 1

Related Questions