I have an Android app that uses LogBack library for logging. I need to delete the log file when it reaches 2MB (I don't need it rotated).
Here's my configuration file:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/mnt/sdcard/app/app.log</file>
<append>true</append>
<triggeringPolicy class="com.app.utils.LogbackSizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Since there seems to be an bug with SizeBasedTriggeringPolicy (http://jira.qos.ch/browse/LOGBACK-74) as you can see, I have written my own trigger code:
package com.app.utils;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;
import ch.qos.logback.core.util.FileSize;
public class LogbackSizeBasedTriggeringPolicy<E> extends
SizeBasedTriggeringPolicy<E> {
@Override
public boolean isTriggeringEvent(File activeFile, E event) {
return activeFile.length() >= FileSize.valueOf(getMaxFileSize())
.getSize();
}
}
The logging works fine - but the triggeringPolicy code is never fired and therefore the log file grows beyond 2MB (which is not what I want).
I'm new to LogBack - any help would be appreciated.
Your config is missing the <rollingPolicy> element, as would've been indicated in logcat:
... I/System.out( 614): 20:29:27,999 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property I/System.out( 614): 20:29:28,086 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - No RollingPolicy was set for the RollingFileAppender named FILE I/System.out( 614): 20:29:28,087 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - For more information, please visit http://logback.qos.ch/codes.htmlrfa_no_rp ...
The logback manual for RollingFileAppender states:
To be of any use, a
RollingFileAppendermust have both aRollingPolicyand aTriggeringPolicyset up. However, if itsRollingPolicyalso implements theTriggeringPolicyinterface, then only the former needs to be specified explicitly.
So, you can either (1) add a <rollingPolicy> element to your appender configuration, or (2) implement both RollingPolicy and TriggeringPolicy interfaces and then specify that implementation as the class in <rollingPolicy class='com.example.MyRollingPolicy'> (in which case a triggering policy is unnecessary since MyRollingPolicy itself handles it).
What I need is the log file to be truncated - not interested in rolling.
However as user46874's says, the TriggerPolicy must be accompanies with a RollingPolicy. So I extended FixedWindowRollingPolicy and overrode rollover() with my own implementation which simply deletes the log file like so:
package com.app.utils;
import java.io.File;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import ch.qos.logback.core.rolling.RolloverFailure;
public class LogbackRollingPolicy extends FixedWindowRollingPolicy{
@Override
public void rollover() throws RolloverFailure {
File file = new File(getActiveFileName());
file.delete();
}
}
And my configuration looks like:
<rollingPolicy class="com.app.utils.LogbackRollingPolicy">
</rollingPolicy>
Not sure if that is the "proper" way to go about it, but it works fine.
I've accepted user46874's answer because it pointed me in the correct direction and added this answer for anyone who might be have the same issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With