Java – common logging and log4j property files
I try to use log4j through commons logging if the log4j property file is not called log4 Properties, you will encounter problems
My code is simple:
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class LogMePlease { static Log l = LogFactory.getLog(LogMePlease.class); public static void main(String [] args) { l.warn("Hello World!"); } }
On the way to my course, I have: commons logging The properties file contains the following entries
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger log4j.configuration=log4j-test.properties
And log4j test Properties file
When I run this code, I get
log4j:WARN No appenders Could be found for logger (LogMePlease). log4j:WARN Please initialize the log4j system properly.
If I put log4j test Rename the properties file to log4j Properties, then everything is normal Therefore, the problem is how to set up common logging to use log4j Any name of the properties file
Solution
File commons logging Properties can only be read from the public log record, and log4j will look for log4j in the system properties configuration.
Therefore, you must use - dlog4j. On the command line configuration = log4j-test. Properties specify them as JVM options, or you must call system. Net before calling any logging method for the first time, which is often difficult to implement setproperty() ).
Note: if possible, use XML to configure log4j xml; It is simple and powerful for configuring log4j