How to set maximum log file size on android

I use microlog4android to enter the file. The question is how to set the maximum file size?

microlog4android The FileAppender class has two methods: getLogSize (which always returns -1) and clears. I can clear the log when it reaches a certain size, but getLogSize does not work.

Is there a better, more mature Android registration solution that I don't know about?

+1
source share
2 answers

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> <!-- ######################################### # Max of 2 backup zip (plus uncompressed # working file, ${dest.dir}/test.log) ######################################### --> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${dest.dir}/test.%i.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>2</maxIndex> </rollingPolicy> <!-- ######################################### # Rollover when file size reaches 5MB. # We're using our custom policy here that # works around a bug (LBCORE-123). ######################################### --> <!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">--> <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> 
+4
source

Perhaps you can use the regular File apis to extract the length of the file and then delete / rename it before initializing Logger if it exceeds the size limit.

0
source

All Articles