By setting the
attribute ‘monitorInterval’ in log4j configuration file, you can reflect the
changes at runtime.
<?xml
version="1.0" encoding="UTF-8"?>
<Configuration
monitorInterval="30">
...
</Configuration>
In above
example, monitorInterval is set to 30 seconds, so for every 30 seconds the
configuration file is checked for modifications. Minimum interval time to set
is 5 seconds.
For example
log4j2.xml
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="DEBUG" monitorInterval="30"> <Appenders> <Console name="my_console_appender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Root level="trace"> <AppenderRef ref="my_console_appender" /> </Root> </Loggers> </Configuration>
HelloWorld.java
package log4j_tutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HelloWorld { private static final Logger log = LogManager.getLogger(); public static void printData() { log.trace("Trace Message!"); log.debug("Debug Message!"); log.info("Info Message!"); log.warn("Warn Message!"); log.error("Error Message!"); log.fatal("Fatal Message!"); } public static void main(String args[]) throws InterruptedException { printData(); for(int i=0; i<50; i++){ log.info("Information message " + i); Thread.sleep(1000); } printData(); } }
Run above
application and change the log level from ‘trace’ to ‘warn’. After 30 seconds
the changes will reflect and you will see following output.
15:42:01.671 [main] TRACE log4j_tutorial.HelloWorld - Trace Message! 15:42:01.672 [main] DEBUG log4j_tutorial.HelloWorld - Debug Message! 15:42:01.672 [main] INFO log4j_tutorial.HelloWorld - Info Message! 15:42:01.672 [main] WARN log4j_tutorial.HelloWorld - Warn Message! 15:42:01.672 [main] ERROR log4j_tutorial.HelloWorld - Error Message! 15:42:01.672 [main] FATAL log4j_tutorial.HelloWorld - Fatal Message! 15:42:01.672 [main] INFO log4j_tutorial.HelloWorld - Information message 0 15:42:02.673 [main] INFO log4j_tutorial.HelloWorld - Information message 1 15:42:03.674 [main] INFO log4j_tutorial.HelloWorld - Information message 2 15:42:04.674 [main] INFO log4j_tutorial.HelloWorld - Information message 3 15:42:05.676 [main] INFO log4j_tutorial.HelloWorld - Information message 4 15:42:06.677 [main] INFO log4j_tutorial.HelloWorld - Information message 5 15:42:07.679 [main] INFO log4j_tutorial.HelloWorld - Information message 6 15:42:08.679 [main] INFO log4j_tutorial.HelloWorld - Information message 7 15:42:09.680 [main] INFO log4j_tutorial.HelloWorld - Information message 8 15:42:10.681 [main] INFO log4j_tutorial.HelloWorld - Information message 9 15:42:11.682 [main] INFO log4j_tutorial.HelloWorld - Information message 10 15:42:12.682 [main] INFO log4j_tutorial.HelloWorld - Information message 11 15:42:13.682 [main] INFO log4j_tutorial.HelloWorld - Information message 12 15:42:14.683 [main] INFO log4j_tutorial.HelloWorld - Information message 13 15:42:15.684 [main] INFO log4j_tutorial.HelloWorld - Information message 14 15:42:16.686 [main] INFO log4j_tutorial.HelloWorld - Information message 15 15:42:17.686 [main] INFO log4j_tutorial.HelloWorld - Information message 16 15:42:18.687 [main] INFO log4j_tutorial.HelloWorld - Information message 17 15:42:19.689 [main] INFO log4j_tutorial.HelloWorld - Information message 18 15:42:20.690 [main] INFO log4j_tutorial.HelloWorld - Information message 19 15:42:21.691 [main] INFO log4j_tutorial.HelloWorld - Information message 20 15:42:22.692 [main] INFO log4j_tutorial.HelloWorld - Information message 21 15:42:23.692 [main] INFO log4j_tutorial.HelloWorld - Information message 22 15:42:24.693 [main] INFO log4j_tutorial.HelloWorld - Information message 23 15:42:25.694 [main] INFO log4j_tutorial.HelloWorld - Information message 24 15:42:26.695 [main] INFO log4j_tutorial.HelloWorld - Information message 25 15:42:27.697 [main] INFO log4j_tutorial.HelloWorld - Information message 26 15:42:28.698 [main] INFO log4j_tutorial.HelloWorld - Information message 27 15:42:29.699 [main] INFO log4j_tutorial.HelloWorld - Information message 28 15:42:30.700 [main] INFO log4j_tutorial.HelloWorld - Information message 29 15:42:31.700 [main] INFO log4j_tutorial.HelloWorld - Information message 30 15:42:32.701 [main] INFO log4j_tutorial.HelloWorld - Information message 31 15:42:33.702 [main] INFO log4j_tutorial.HelloWorld - Information message 32 15:42:34.703 [main] INFO log4j_tutorial.HelloWorld - Information message 33 2015-10-02 15:42:34,703 Thread-1 DEBUG Reconfiguration started for context 1163157884 (org.apache.logging.log4j.core.LoggerContext@8bd1b6a) 15:42:51.719 [main] WARN log4j_tutorial.HelloWorld - Warn Message! 15:42:51.719 [main] ERROR log4j_tutorial.HelloWorld - Error Message! 15:42:51.719 [main] FATAL log4j_tutorial.HelloWorld - Fatal Message!
As you observe the output, configuration
changes are reflected at run time.
Note:
If you are running your application in
eclipse, you can’t see the changes, unless you update the log4j2.xml file in ${project}/bin,
or ${project}/target/classes.
It is because, Eclipse will copy your
log4j2.xml file from ${project}/src to ${project}/bin. Log4j will load the
log4j2.xml file in the classpath, which is ${project}/bin. If you now modify
the other config file in your src/ folder that may not be copied to the bin/
folder until Eclipse detects that a rebuild is required.
No comments:
Post a Comment