问题描述
我正在使用 microlog4android 登录到文件.问题是如何设置最大文件大小?
microlog4android FileAppender 类有两个方法:getLogSize(总是返回 -1)和 clear.我可以在达到特定大小时清除日志,但 getLogSize 似乎不起作用.
是否有我不知道的更好、更成熟的 android 日志记录解决方案?
推荐答案
你可以使用logback-android 与 RollingFileAppender 和 SizeBasedTriggeringPolicy,但您应该注意错误(src) 可能导致您的工作日志文件超过最大文件大小.一种解决方法是将触发策略子类化以绕过错误:
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()); } }
这是一个示例配置(基于 Logback manual 并包括上面的解决方法),您可以将其放入您的AndroidManifest.xml.我在 Android 4.0.3 模拟器中对此进行了测试,但我想它也适用于早期版本.
<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's (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>
其他推荐答案
也许你可以使用普通的File apis来检索文件长度,如果超过大小限制,然后在初始化Logger之前删除/重命名它.
问题描述
I'm using microlog4android to log to file. The question is how can set maximum file size?
microlog4android FileAppender class has two methods: getLogSize (which always return -1 ) and clear. I could clear log when it reaches certain size but getLogSize doesn't seem to work.
Is there any better, more mature android logging solution that I'm not aware of?
推荐答案
You can use logback-android with the RollingFileAppender and SizeBasedTriggeringPolicy, but you should be aware of a bug (src) that may cause your working log file to exceed the max file size. A workaround is to subclass the triggering policy to bypass the bug:
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's an example config (based on the Logback manual and including the workaround above) that you can put into your AndroidManifest.xml. I tested this in Android 4.0.3 emulator, but I imagine 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's (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>
其他推荐答案
May be you can use normal File apis to retrieve file length and then delete/rename it before initializing Logger if it exceeds size limit.