I want to change the log level dynamically based on TEST/PROD server accessing the application.
Our web app has a lot of DEBUG and INFO,error log statements. Once we deploy the .war file to the test / production servers,
the log4j.xml also gets deployed which results in a lot of DEBUG and INFO statements in the log files.
How to show only error logs in PROD server and debug and error logs in DEV/TEST server. Apache Tomcat is the server we are using to deploy the application.
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- This appender logs to the console -->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%-5p] [%c{1}] [%M:%L] - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.home}/logs/myProject.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<!-- Set "debug" log level for project code -->
<logger name="com.choonchernlim.myproject">
<level value="info"/>
</logger>
<!-- Set "info" log level for Spring framework -->
<logger name="org.springframework">
<level value="info"/>
</logger>
<!-- Set "debug" log level for Hibernate framework -->
<logger name="org.hibernate">
<level value="debug"/>
</logger>
<root>
<!-- The default log level is "warn" -->
<priority value="warn"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
Included below tags in web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
One approach i could think of is to edit setenv.bat from apachetomcatserver\bin and add loglevel. But how to set loglevels dynamically in the log4j.xml file???
in tomcatserver : catalinahome\bin\setenv.bat
set loglevel=error
How to dynamically set the loglevel so that when application is deployed in TEST all logs are shown and when deployed in PROD only error log messages are shown in the log file.
I am using log4j1.2,spring,java.