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?
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.
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.
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 ...
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).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With