Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j.xml in client jars

Tags:

java

log4j

client

I have some jar files that will be distributed to clients that are using log4j for logging. My question is should I include a log4j.xml configuration in the jar file or have the client provide one if they want logging?

My feeling is to leave the log4j.xml configuration file out of the client jars, since the apache jar files all come with log4j logging, but sans log4j.xml.

like image 288
Mike Pone Avatar asked Jun 16 '09 16:06

Mike Pone


People also ask

Where should log4j XML be placed?

xml parameters, Locate the log4j. xml file under the oarm/WEB-INF/classes/ directory. Update the log output path for each appender.

How do you get all Appenders in log4j2?

Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();


3 Answers

Yes, leave it out. It's an utter nuisance when your log4j configuration file is ignored because one of the 60 third-party libraries of your app contains its own.

like image 147
mfx Avatar answered Oct 01 '22 13:10

mfx


The good thing about log4j in your case is that your jar really shouldn't have to worry about it. The basic use case of log4j is:

  1. Obtain a logger object for the current class
  2. Call one of the methods on that logger, such as debug("some message");

If the jars you are shipping are to be used by a larger application, then ideally your code will only do the two steps listed above. In this way, your code will simply obtain logger objects from the already-configured log4j instance in the client's application. Your production code is then decoupled from having to know how to configure log4j.

Any logging you need to see for your development of the jars can be accomplished by configuring a log4j instance in unit test setUp() methods or something similar that won't get bundled with the production code going to the client.

like image 33
alt_tab Avatar answered Oct 01 '22 12:10

alt_tab


I would put a default log4j configuration that you expect will be useful to your clients in the documentation. This way interested people can see what logging options you have (usually certain classes have more interesting log messages, from the user's perspective). I find it annoying when I have a third-party lib using log4j and it has no documentation, and there are log messages filling my screen and I have to try to figure out how to enable or suppress certain log messages.

like image 37
Mr. Shiny and New 安宇 Avatar answered Oct 01 '22 12:10

Mr. Shiny and New 安宇