[logback-user] logback and manual rollover (from java)
Ceki Gülcü
ceki at qos.ch
Fri Jul 23 10:36:42 CEST 2010
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%28java.lang.String%29
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.
>
More information about the Logback-user
mailing list