如何设置登录Android文件最大尺寸 [英] How can I set log file max size on android

查看:111
本文介绍了如何设置登录Android文件最大尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用microlog4android登录到文件中。现在的问题是如何设置的最大文件大小是多少?

I'm using microlog4android to log to file. The question is how can set maximum file size?

microlog4android FileAppender类有两个方法:getLogSize(它总是返回-1)和明确的。我可以清除日志,当它达到一定的规模,但getLogSize似乎并没有工作。

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?

推荐答案

您可以使用 的logback-机器人 与<一个href="http://tony19.github.com/logback-android/doc/1.0.0-4/ch/qos/logback/core/rolling/RollingFileAppender.html"><$c$c>RollingFileAppender和<一href="http://tony19.github.com/logback-android/doc/1.0.0-4/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.html"><$c$c>SizeBasedTriggeringPolicy,但你应该知道一个错误的(<一href="https://github.com/tony19/logback-android/blob/master/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java#L57">src)这可能会导致你的工作日志文件超过最大文件大小一种解决方法是继承触发策略绕过漏洞:

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());
    }
}


下面是一个例子配置(根据的logback 手动并包括上述的解决方法),你可以把你的AndroidManifest.xml中。我的Andr​​oid 4.0.3模拟器测试这一点,但我想这也将工作在早期版本。


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>

这篇关于如何设置登录Android文件最大尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆