[logback-user] Help needed

Tony Trinh tony19 at gmail.com
Fri Jun 28 19:19:29 CEST 2013


On Thu, Jun 27, 2013 at 12:55 AM, Jagmohan Singh <jagmohan.singh.1 at gmail.com
> wrote:

>
> I have used
> logback-android-1.0.10-2.jar
> slf4j-api-1.7.5.jar
>
> Is it one of the stable versions that I am using? If not, is it possible
> for you to suggest the stable version?
>
>
Yes, logback-android-1.0.10-2.jar is the latest stable release.


> Also, if everything is fine, I would like to know how can I configure a
> Rolling file appender with rolling time of 4 hours? Is it possible?
>
>
Not out of the box. Currently, the TimeBasedRollingPolicy[1] rolls over
files at the beginning of a time period (rather than at specific
multiples). There might be an easier (or more correct) way to accomplish
your goal, but I suggest overriding the period computation within the
triggering policy of TimeBasedRollingPolicy.

Here's some *untested* code (at the moment, I don't have my development
environment handy to verify any of this).

Make this triggering-policy class available on your classpath (e.g., by
compiling it into a JAR, and then putting the JAR in your libs directory at
the root of your apk):

package com.example;

public class MyTimeBasedTriggeringPolicy<E> extends
DefaultTimeBasedFileNamingAndTriggeringPolicy<E> {
  private int multiple = 1;

  public void setTimeMultiple(int multiple) {
    this.multiple = multiple;
  }

  @Override
  protected void computeNextCheck() {
    super.computeNextCheck();
    nextCheck *= multiple;
  }
}

Then, in your logback.xml:

<configuration>
  <appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- hourly rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd_HH}.log</fileNamePattern>

      <!-- make that every 4 hours -->
      <timeBasedFileNamingAndTriggeringPolicy
class="com.example.MyTimeBasedTriggeringPolicy">
          <timeMultiple>4</timeMultiple>
      </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>


[1] http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130628/4ef3cb0b/attachment.html>


More information about the Logback-user mailing list