[logback-user] Switch logging to different files at runtime
lee json
jsonlee.ft at gmail.com
Tue May 20 21:29:26 CEST 2014
I test to run a sample logging message to a file. But I am wondering
how can I switch logging to different files at runtime? For example,
initially the message is logged to <log_dir>/<file_a>.log. Later on I
would like the application to log message to <log_dir>/<file_b>.log.
Is it doable with logback lib?
I want to log message to logs/<parent_key_id>/<key_id>...log. Is
RollingPolicy configured to start up once? How can I change logging to
another log file such as logs/<parent_key_id1>/<key_id1>...log when
certain condition is met?
Thanks
The current setting:
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" debug="false">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="sample.logging.MyRollingPolicy">
<FileNamePattern>%keyId_%d.log.gz</FileNamePattern>
<MaxHistory>60</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<!-- http://logback.qos.ch/manual/layouts.html#PatternLayout -->
<Pattern>[%4p] [%d{ISO8601}] [%t] %c{1}: %m%n</Pattern>
</layout>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>
MyRolling.scala
package sample.logging
import java.io.File
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
class MyRollingPolicy[E] extends TimeBasedRollingPolicy[E] {
override def setFileNamePattern(fnp: String) {
try{
println("fnp passed in: "+fnp)
val fnp1 = fnp.replace("%keyId", "k_1_3")
println("[after replaced] fnp1 now is "+fnp1)
val f = new File("logs/" + "parent_key_id");
// Logback will create any necessary parent paths.
super.setFileNamePattern(f.getAbsolutePath() + "/" + fnp1);
} catch {
case e: Exception => {
println("something go wrong..."+e)
throw new RuntimeException(e);
}
}
}
}
More information about the Logback-user
mailing list