0

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.

coder
  • 8,346
  • 16
  • 39
  • 53
nan
  • 191
  • 3
  • 10

2 Answers2

0

You would have to create different log4j.xml-files for the corresponding environments. And then during startUp, you can simply provide the path to the log4j.xml configuration it should use.

Application parameter:

-Dlog4j.configuration=path/to/env/folder/log4j.xml
  • But i have log4j.xml in web-inf folder of my application. How to know if i'm using test server or prod server to deploy the application to read that particular xml files? – nan May 04 '18 at 14:59
  • You can remove it there and add it to the VM options as I had explained in my comment. @nan – Babajide M. Moibi May 04 '18 at 15:19
0

If you are using spring framework, you can use logback for logging configuration and then you can control log level based on your active spring profile.

You can get some help form following examples-

http://www.baeldung.com/spring-profiles

https://springframework.guru/using-logback-spring-boot/

Avhi
  • 806
  • 2
  • 15
  • 29