[logback-user] Setting logback.xml parameters programmatically

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


Hi,
 
Need some pointers regarding the following:
 
Requirement:
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 {
@Override
public void setMaxFileSize(String maxFileSize) { 
super.setMaxFileSize(maxFileSize);
}
}
 
For Number Of Log Files:
LoggerRollingPolicy rollingPolicy = new LoggerRollingPolicy();
rollingPolicy.setMaxIndex(<User defined number of files>);
 
public class LoggerRollingPolicy extends FixedWindowRollingPolicy {
@Override
public void setMaxIndex(int maxIndex) {
super.setMaxIndex(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
ci.autoConfig();
 
 
logback.xml
<configuration>
 
       <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
              <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
              <encoder>
                     <pattern>%d [%thread] %-5level %class{0} %msg%n</pattern>
              </encoder>
       </appender>
 
       <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
              <file>C:/test.log</file>
              <rollingPolicy class="com.cisco.vnmc.hcloud.cpm.util.LoggerRollingPolicy">               
                     <fileNamePattern>C:/test.%i.log</fileNamePattern>                    
                     <minIndex>1</minIndex>
              </rollingPolicy>
              
              <triggeringPolicy class="com.cisco.vnmc.hcloud.cpm.util.LoggerTriggeringPolicy">
              </triggeringPolicy>
              
              <encoder>
                     <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %class{0} %msg%n</pattern>
              </encoder>
       </appender>
 
       <root level="DEBUG">
              <appender-ref ref="STDOUT" />
              <appender-ref ref="FILE" />
       </root>
</configuration>
 
Thanks,
Kriti



 
=====-----=====-----=====
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