You can use logback-android with RollingFileAppender and SizeBasedTriggeringPolicy , but you should be aware of the error ( src ), which could cause your working log file to exceed the maximum file size. The workaround is to subclass the startup policy to get around the error:
package com.example; import java.io.File; import ch.qos.logback.core.util.FileSize; public class SizeBasedTriggeringPolicy<E> extends ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy<E> { @Override public boolean isTriggeringEvent(final File activeFile, final E event) { return (activeFile.length() >= FileSize.valueOf(getMaxFileSize()).getSize()); } }
Here is an example config (based on the Logback manual and including a workaround above) that you can put in your AndroidManifest.xml. I tested this in Android 4.0.3 emulator, but I think it would also work in earlier versions.
<logback> <configuration debug="true"> <appender name="LOGCAT" class="ch.qos.logback.classic.android.LogcatAppender" > <encoder> <pattern>[%method] %msg%n</pattern> </encoder> </appender> <property name="dest.dir" value="/sdcard/test/" /> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${dest.dir}/test.log</file> <append>false</append> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${dest.dir}/test.%i.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>2</maxIndex> </rollingPolicy> <triggeringPolicy class="com.example.SizeBasedTriggeringPolicy"> <maxFileSize>5MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <logger name="com.example.HelloAndroidActivity" level="TRACE"> <appender-ref ref="FILE" /> </logger> <root level="DEBUG" > <appender-ref ref="LOGCAT" /> </root> </configuration> </logback>
user748113
source share