Log4j automatically loads configuration file named log4j.properties or log4j.xml on one of top directories in classpath at initialization. For example, if your classpath is /foo:/foo/bar.jar, /foo/log4j.properties would be automatically configured.
But if you should use different name or position, for example /foo/log4j-test.properties or /foo/baz/qux/log4j.properties, there are two methods.
Using log4j.configuration system property
Load the application specifying explicitly the Log4j configuration file to use using log4j.configuration system property.
java -Dlog4j.configuration=baz/qux/log4j.properties MyApplication.java
Note that the value should be relative to classpath. It is not /foo/baz/qux/log4j.properties but baz/qux/log4j.properties.
Using Configurator.configure method
Before configruing proper Configurator, call static LogManager.resetConfiguration method.
import org.springframework.core.io.ClassPathResource;
...
Properties props = new Properties();
props.load((new ClassPathResource("baz/qux/log4j.properties"))
.getInputStream());
org.apache.log4j.LogManager.resetConfiguration();
org.apache.log4j.PropertyConfigurator.configure(props);
1 comments:
Thanks Sangmoon
Post a Comment