[logback-user] Setting logback.xml parameters programmatically

Kriti Wadhwa kriti.wadhwa at tcs.com
Mon Jul 8 14:13:30 CEST 2013

Need some pointers regarding the following:
For logging, user configures following parameters from Application (UI):
Log Level
File Size
Number Of Log Files
Time Zone
Above mentioned parameters needs to be programmatically set to logback.xml using ch.qos.logback API's.
We tried to implement in the following way but there seems to be some missing link, please suggest how to achieve the above mentioned requirement.
Do we need to link the policy with File Appender ? 
For Log Level:
Logger logger = (Logger)LoggerFactory.getLogger("<package>");
logger.setLevel(<User defined log level>);
For File size:
LoggerTriggeringPolicy triggerPolicy = new LoggerTriggeringPolicy();
triggerPolicy.setMaxFileSize(<User defined file size>);
public class LoggerTriggeringPolicy extends SizeBasedTriggeringPolicy {
public void setMaxFileSize(String maxFileSize) { 
For Number Of Log Files:
LoggerRollingPolicy rollingPolicy = new LoggerRollingPolicy();
rollingPolicy.setMaxIndex(<User defined number of files>);
public class LoggerRollingPolicy extends FixedWindowRollingPolicy {
public void setMaxIndex(int maxIndex) {
For Time Zone:
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
ContextInitializer ci = new ContextInitializer(lc);
lc.reset(); //This will pick Time Zone from the host JVM
       <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
              <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
                     <pattern>%d [%thread] %-5level %class{0} %msg%n</pattern>
       <appender name="FILE"
              <rollingPolicy class="com.cisco.vnmc.hcloud.cpm.util.LoggerRollingPolicy">               
              <triggeringPolicy class="com.cisco.vnmc.hcloud.cpm.util.LoggerTriggeringPolicy">
                     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %class{0} %msg%n</pattern>
       <root level="DEBUG">
              <appender-ref ref="STDOUT" />
              <appender-ref ref="FILE" />

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/20130708/1988141e/attachment-0001.html>

More information about the Logback-user mailing list