Switch logging to different files at runtime

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); } } } }

You can use sifting appender to change log file name dynamically based on values in the MDC. For instance you could have one log file per user. Or one line file per web session. David
On 20 May 2014, at 20:29, lee json <jsonlee.ft@gmail.com> wrote:
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); } } } } _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user

Changing to use sift appender is working with content similar to below Thanks for the advice. <configuration> <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator> <key>log.file</key> <defaultValue>unknown</defaultValue> </discriminator> <sift> <appender name="FILE-${log.file}" class="ch.qos.logback.core.FileAppender"> <file>/path/to/log/${log.file}.log</file> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern><!-- ... log pattern ... --></pattern> </layout> </appender> </sift> </appender> <root level="ALL"> <appender-ref ref="SIFT" /> </root> </configuration> On 21 May 2014 03:40, David Roussel <nabble@diroussel.xsmail.com> wrote:
You can use sifting appender to change log file name dynamically based on values in the MDC.
For instance you could have one log file per user. Or one line file per web session.
David
On 20 May 2014, at 20:29, lee json <jsonlee.ft@gmail.com> wrote:
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); } } } } _______________________________________________ Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
Logback-user mailing list Logback-user@qos.ch http://mailman.qos.ch/mailman/listinfo/logback-user
participants (2)
-
David Roussel
-
lee json