2

I am using the logback as implementation with SL4j interface. Here is the configuration

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/prod.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/prod.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>700MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.ecom" additivity="false" level="ERROR"> <appender-ref ref="FILE" /> </logger> 

I want to change the level to Info or debug at runtime through configuration or external property change in production without restarting the server. Is it possible ?

FYI ,I am using Weblogic as application server and also Spring frameworks for other purposes >

2

3 Answers 3

2

What you can try is to include another file (outside your webapp) that overrides the config in your logback.xml.

<configuration scan="true" scanPeriod="30 seconds"> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/prod.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/prod.%d{yyyy-MM-dd}.%i.log</FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>700MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.ecom" additivity="false" level="ERROR"> <appender-ref ref="FILE" /> </logger> <include optional="true" file="/tmp/logbackDynamic.xml"/> </configuration> 

Then your /tmp/logbackDynamic.xml could look like this if you want DEBUG loggings for com.ecom.SomeClass

<included> <logger name="com.ecom.SomeClass" level="DEBUG"> <appender-ref ref="FILE" /> </logger> </included> 

Now Logback will check every 30 seconds if you changed your config in /tmp/logbackDynamic.xml, and reload it. If you want to return the original log level, just remove the line between the tags and Logback will ignore the DEBUG level. You could even delete the file because of optional="true".

Sign up to request clarification or add additional context in comments.

Comments

1

There are two ways to externalize the logger level

  1. One at with system level property i.e. java -Dlogback.configurationFile=/pathToconfig.xml. See How to externalize the log level

  2. <root level="${log.level:-Error}">. Then set the system level property -Dlog.level=DEBUG

Comments

0

Variable substitution is your friend here. Default values for variables should be helpful as well.

See also variable scoping.

2 Comments

in the boh the examples you mentioned, variable substitution is used to specify where log file will be created. How it can be used to change the log level at run time ? Do you mean i should create external properties file like <property file="src/main/java/chapters/configuration/variables1.properties" /> which will contain some property like LOG_LEVEL=DEBUG and then use it <root level="${LOG_LEVEL}"> ? `
Variable substitution works for the "level" attribute as well. I also modified my answer above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.