Rahul
Rahul

Reputation: 131

how to set dynamic log level in spring-mvc application?

i want to set log level dynamic in my spring-mvc project. i want to set it dynamically so that without restarting server i can change log level. we can user apllication.propertes or get log level from database. my server contain too many logs of my application. i want to stop it and whenever i want to show logs i just change log level from database or any other way. But i want to achieve this process without re-starting my server.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="${catalina.base}/logs" />


    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">

            <Pattern>
                %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
            </Pattern>

        </layout>
    </appender>
    <root level="error"> 
        <appender-ref ref="STDOUT" />
    </root>


  <appender name="FILE2" class="ch.qos.logback.core.FileAppender">

                 <file>${DEV_HOME}/SMPPIN.log</file>
               <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>

       <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <!-- or whenever the file size reaches 100MB -->
        <maxFileSize>1MB</maxFileSize>
      </timeBasedFileNamingAndTriggeringPolicy>


                  </rollingPolicy>
        <!-- <param name="File" value="SMPPIN.log"/> orther options ${user.home}
                 <param name="MaxFileSize" value="15000KB"/>
                 <param name="MaxBackupIndex" value="30"/>
                        <param name="append" value="true" /> -->

    <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>


     <logger name="SMPPINFO" level="info" additivity="false">

          <appender-ref ref="FILE2"/>         
        </logger>

</configuration>

Upvotes: 2

Views: 2272

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

If you use Spring boot, then this feature is available with spring-actuator.

Just plug-in the actuator (via your build system) and you'll get some endpoints accessible to your application, one of them allows changing log level dynamically without restart.

Of course, when the restart will be done, the configuration will change back.

The relevant example can be found here for example.

Bottom line, just call the following endpoint, and you're set

curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' http://localhost:8080/loggers/ROOT

If you don't use spring boot, this feature is not available by default, so you'll have to do it by yourself.

There are many different approaches, here are some of them:

  • Since you're using logback, you can take advantage of its JMX configurator: See an example Here The idea is simple: you configure logback to expose some JMX mbeans and connect to your application via jconsole/jvisualvm or any other client of your choice that can work with JMX

  • You can use a programmatic access to the logger and create some endpoint (HTTP) that will just use something like:

    Logger logger = ... ;

    logger.setLevel(Level.DEBUG);

  • Works in conjunction with the JMX method. You can use "jolokia" project to open http API over JMX if you can't access JMX easily.

Upvotes: 3

Related Questions