
Hi, I used log4j before and I triggered my RollingFileAppender rollover() manually in some places (batches). I need to create seperate logfiles for each batch run. I want it to rollover using the FixedWindowRollingPolicy if the logfile exceeds 10MB and at the start of each batch run. I've migrated to logback now and it works well, but I can't figure out how to rollover my RollingFileAppender manually. What I have now : <appender name="EntityToCSV" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>/home/tomcat/overname_test_logs/EntityToCSV.log</File> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}: %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <maxIndex>9</maxIndex> <FileNamePattern>/home/tomcat/overname_test_logs/EntityToCSV.log.%i</FileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <logger name="nl.realworks.apps.overname.EntityToCSV" level="INFO"> <appender-ref ref="EntityToCSV"/> </logger> in EntityToCSV.java : private static final Logger LOGGER = LoggerFactory.getLogger(EntityToCSV.class); public static void main(String[] args) throws Exception { OvernameUtils.manualRollover(LOGGER); // do stuff and log it in OvernameUtils.java : public static void manualRollover(Logger logger) { Utils.assertNotNull("logger is null.", logger); if (logger instanceof RollingFileAppender) { // <--- FAIL ! try { ((RollingFileAppender)logger).rollover(); } catch (Exception e) { logger.error("rollover failed.", e); } return; } String message = logger.getName() + " is not an instance of RollingFileAppender."; logger.error(message, new RuntimeException(message)); } This won't work of course as Logger != Appender. I have no idea how to get from the Logger to its Appender(s). In log4j you could just do getAppender() but this is not possible with org.slf4j.Logger Perhaps this is a totally wrong approach. Any tips would be greatly appreciated. -- View this message in context: http://old.nabble.com/logback-and-manual-rollover-%28from-java%29-tp29237145... Sent from the Logback User mailing list archive at Nabble.com.

When you are sure that the underlying framework is logback, you can cast an org.slf4j.Logger instance into ch.qos.logback.classic.Logger. The latter has a method to retrieve an appender by name [1]. I hope this helps, [1] http://logback.qos.ch/apidocs/ch/qos/logback/classic/Logger.html#getAppender... On 23/07/2010 10:29 AM, lgonggr wrote:
Hi,
I used log4j before and I triggered my RollingFileAppender rollover() manually in some places (batches). I need to create seperate logfiles for each batch run. I want it to rollover using the FixedWindowRollingPolicy if the logfile exceeds 10MB and at the start of each batch run. I've migrated to logback now and it works well, but I can't figure out how to rollover my RollingFileAppender manually.
What I have now : <appender name="EntityToCSV" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>/home/tomcat/overname_test_logs/EntityToCSV.log</File> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}: %m%n</pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <maxIndex>9</maxIndex>
<FileNamePattern>/home/tomcat/overname_test_logs/EntityToCSV.log.%i</FileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender>
<logger name="nl.realworks.apps.overname.EntityToCSV" level="INFO"> <appender-ref ref="EntityToCSV"/> </logger>
in EntityToCSV.java :
private static final Logger LOGGER = LoggerFactory.getLogger(EntityToCSV.class);
public static void main(String[] args) throws Exception {
OvernameUtils.manualRollover(LOGGER);
// do stuff and log it
in OvernameUtils.java :
public static void manualRollover(Logger logger) { Utils.assertNotNull("logger is null.", logger); if (logger instanceof RollingFileAppender) { //<--- FAIL ! try { ((RollingFileAppender)logger).rollover(); } catch (Exception e) { logger.error("rollover failed.", e); } return; } String message = logger.getName() + " is not an instance of RollingFileAppender."; logger.error(message, new RuntimeException(message)); }
This won't work of course as Logger != Appender. I have no idea how to get from the Logger to its Appender(s). In log4j you could just do getAppender() but this is not possible with org.slf4j.Logger
Perhaps this is a totally wrong approach. Any tips would be greatly appreciated.

It works, thanks a billion ! :) I'm so happy I'm gonna dance a little jig now :)
Date: Fri, 23 Jul 2010 10:36:42 +0200 From: ceki@qos.ch To: logback-user@qos.ch Subject: Re: [logback-user] logback and manual rollover (from java)
When you are sure that the underlying framework is logback, you can cast an org.slf4j.Logger instance into ch.qos.logback.classic.Logger. The latter has a method to retrieve an appender by name [1].
I hope this helps,
[1] http://logback.qos.ch/apidocs/ch/qos/logback/classic/Logger.html#getAppender...
_________________________________________________________________ Een netbook met Windows 7? Hier vind je alles dat je moet weten. http://www.windows.nl/netbook
participants (3)
-
Ceki Gülcü
-
lgonggr
-
Lodewijk Gonggrijp