How to dynamically change log level in SLF4j or Log4J

I recently came across a situation where Application Loglevel changes dynamically. The application administrator can set it to INFO / DEBUG / WARN from the front. Based on the log level, select it so that the application log is changed.

I am sure that registrars support this scenario, but are not sure how I can achieve this. If any of you have an idea / thoughts, please let me know.

Thanks in advance for your help.

-Narendra

+14
java logging slf4j log4j
source share
4 answers

Consider the journal http://logback.qos.ch/ - "the successor to the popular log4j project, gathering when log4j leaves." If specified by the instruction, logback-classic scans the changes in the configuration file and automatically reconfigures when the configuration file is changed. In addition, you can manage logging levels using JMX.

+9
source share

Dynamically it is not possible to change the log level in slf4j, but some servers for slf4j support it, including log4j.

This solution worked for me:

org.apache.log4j.Logger logger4j = org.apache.log4j.Logger.getRootLogger(); logger4j.setLevel(org.apache.log4j.Level.toLevel("ERROR")); 

(Source: http://prateep.info/2015/12/12/Dynamically-change-log-level-in-SLF4j-Log4J-with-Standalone-Java-Class/ )

The disadvantage of this solution is that it directly uses the backend, which you should not do when using slf4j, because the slf4j point should provide an abstraction from the specific backend you are using.

+14
source share

I had to do this once with log4j. The only way to figure out how to do this is to call getAllAppenders on the Logger object. Then swipe through the apps. If they extend the AppenderSkeleton class (they should), they will have a setThreshold method. Call this method with a new level as a parameter. Subsequent calls to the registrar should use a new level. This will set the level in memory, but not in your log4j configuration file. You might want to do this if it is not automatically changed when the administrator changes the level through the interface. If this is an option, you can consider Evgeny Dorofeevโ€™s next tip and use it. Seems like it would be easier.

+3
source share

Starting with version slf4j 1.7.26 I was able to change the logging level.

Here is logback.xml in the source folder. In the case of the spring boot application, you can place it in the resources folder.

 <configuration scan="true" scanPeriod="20000"> <include file="C:/logback-ext.xml"/> </configuration> 

The logback-ext.xml is stored in any external location. scanPeriod in milliseconds. If unsuccessful, try using include resource instead of include file in logback.xml .

 <included> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> %d{yyyy-MM-dd HH:mm:ss} %-5p [%t] --- %c{1}.%M:%L :: %m %n </pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </included> 

I managed to change the logging level, logging template, attach / detach new ones and add / remove add-on devices.

These are dependencies in pom.xml

 <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> 

Hooray!

+1
source share

All Articles