[logback-user] Using configuration file and programatically changing configuration?
Xavier Outhier
xouthier at yahoo.fr
Mon Aug 12 23:13:37 CEST 2013
Hi Kriti.
This sounds an elegant solution.
Maybe I will try without the properties file, simply using change of the properties in java only
p.setProperty("FILE_SIZE", 200000);
System.setProperties(p);
Thanks,
Xavier.
________________________________
From: Kriti Wadhwa <kriti.wadhwa at tcs.com>
To: logback users list <logback-user at qos.ch>
Cc: logback <logback-user at qos.ch>
Sent: Monday, August 12, 2013 10:28 AM
Subject: Re: [logback-user] Using configuration file and programatically changing configuration?
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:
<?xmlversion="1.0" encoding="UTF-8"?>
<configuration>
<propertyresource="process_settings.properties" />
<appendername="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %class{0} %msg%n</pattern>
</encoder>
</appender>
<appendername="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/xyz.log</file>
<rollingPolicyclass="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/var/log/xyz.log.%i.gz</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>${BACKUP_COUNT}</maxIndex>
</rollingPolicy>
<triggeringPolicyclass="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>
<rootlevel="INFO">
<appender-refref="STDOUT" />
<appender-refref="FILE" />
</root>
<loggername="com.test">
<levelvalue="${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 = newContextInitializer(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
_______________________________________________
Logback-user mailing list
Logback-user at qos.ch
http://mailman.qos.ch/mailman/listinfo/logback-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130812/db6c7527/attachment-0001.html>
More information about the Logback-user
mailing list