Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I put log4j.properties file into library?

Tags:

java

jar

I have a library project. This project uses log4j for logging.

Should I put log4j.properties into generated jar?
If it is not a good practice, could you tell me why?

like image 548
ruslan5t Avatar asked Jun 30 '15 12:06

ruslan5t


People also ask

Where should I place log4j properties file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

What is the use of log4j properties file?

The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

Can we use log4j without properties file?

println() over Log4j, of course for testing purposes, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but the most programmer will agree that they would prefer to use Log4j over println statements, even for test programs if it's easy to ...


2 Answers

You should put your log4j.properties outside your jar file because it will be easier to modify it. If the application does not force a reload of the configuration (you can do that) then a simple restart will load the config (while modifying it inside a jar file means usually a rebuild). If the application does know how to reload it, then your changes will be almost instantly applied. This is very important when you have application in production environment, and you want to change a logging setting (like the log level) in order to get more info but not to stop the application for this (or worse, rebuild it).

like image 143
iullianr Avatar answered Oct 02 '22 17:10

iullianr


No, I would not include log4j.properties in your src/main/resources if it's just a library - people will use your library, and people will have to configure how it should log.

But there is another problem: if you put it in your jar, others will have the log4j in their classpath too. So, if they want to have a custom configuration for log4j (which they will do), the ClassLoader will pick the first file it can find.

Also, if you have a library, I would suggest to use slf4j instead, so your users can choose their favourite logging framework.

For testing purpouses, it's okay to include log4j in src/test/resources.

like image 21
stuXnet Avatar answered Oct 02 '22 18:10

stuXnet