[logback-user] Using configuration file and programatically changing configuration?

Kriti Wadhwa kriti.wadhwa at tcs.com
Mon Aug 12 10:28:43 CEST 2013


Hi Xavier,
 
Have a look at this thread, might be of some help:
http://logback.10977.n7.nabble.com/Basic-example-of-programmatically-setting-the-configuration-td12667.html
 
In my application too there was a similar requirement, I did the following: 
 
Store values to be modified on the fly from UI in a properties(configuration) file, reset the logger context programmatically, logback.xml will read the configuration file and hence the configuration shall be modified. 
(Used Variable Substitution to implement this: http://logback.qos.ch/manual/configuration.html#variableSubstitution).
 
example:
# process_settings.properties : Configuration file (Set the values set from UI here, logback.xml will read this properties file to substitute the variables)
FILE_SIZE=20000000
LOG_LEVEL=INFO
BACKUP_COUNT=4
 
Use these values in logback.xml as follows:
 
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
       <property resource="process_settings.properties" />
 
       <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
              <encoder>
                     <pattern>%d [%thread] %-5level %class{0} %msg%n</pattern>
              </encoder>
       </appender>
 
       <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
              <file>/var/log/xyz.log</file>
              <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">                    
                     <fileNamePattern>/var/log/xyz.log.%i.gz</fileNamePattern>                  
                     <minIndex>1</minIndex>
                     <maxIndex>${BACKUP_COUNT}</maxIndex>                   
              </rollingPolicy>
              
              <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
              <maxFileSize>${FILE_SIZE}</maxFileSize>
              </triggeringPolicy>
              
              <encoder>
                     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %class{0} %msg%n</pattern>
              </encoder>
       </appender>
 
       <root level="INFO">
              <appender-ref ref="STDOUT" />
              <appender-ref ref="FILE" />
       </root>
       <logger name="com.test">
              <level value="${LOG_LEVEL}" />
       </logger>       
       
</configuration>
 
Sample JAVA Code:
 
Set the values set in UI to properties file and reset the logger context as follows.
 
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();                   
ContextInitializer ci = new ContextInitializer(loggerContext);
ci.autoConfig();
Hope this helps..
Regards,
Kriti
 


-----"Logback-user" <logback-user-bounces at qos.ch> wrote: -----


To: logback <logback-user at qos.ch>
From: Xavier Outhier <xouthier at yahoo.fr>
Sent by: "Logback-user" <logback-user-bounces at qos.ch>
Date: 08/09/2013 12:26PM
Subject: [logback-user] Using configuration file and programatically changing configuration?


Hi,


the idea is to have to enable the user to change the config from the UI of the web applications (without accessing the server).

I would like to know if it is possible to have both a configuration file for LogBack for the default usage of the application and the configuration being modified programatically the fly when investigating issue. How to implement that?
On way could be to write on the fly the configuration file when it is set to be reloaded automatically but is there an another way that would pause the reading from the config file and use the API?


Thanks,


Xavier.

_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://mailman.qos.ch/mailman/listinfo/logback-user
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130812/5b5ee0ad/attachment-0001.html>


More information about the Logback-user mailing list