gpt4 book ai didi

android - 登录(安卓): SizeBasedTriggeringPolicy not working

转载 作者:太空狗 更新时间:2023-10-29 14:25:56 26 4
gpt4 key购买 nike

我有一个使用 LogBack 库进行日志记录的 Android 应用程序。我需要在达到 2MB 时删除日志文件(我不需要轮换)。

这是我的配置文件:

<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>

正如您所见,由于 SizeBasedTriggeringPolicy (http://jira.qos.ch/browse/LOGBACK-74) 似乎存在错误,因此我编写了自己的触发代码:

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

}

日志记录工作正常 - 但从未触发 triggeringPolicy 代码,因此日志文件增长超过 2MB(这不是我想要的)。

我是 LogBack 的新手 - 任何帮助将不胜感激。

最佳答案

您的配置缺少 <rollingPolicy>元素,如 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] propertyI/System.out(  614): 20:29:28,086 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - No RollingPolicy was set for the RollingFileAppender named FILEI/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...

logback manual for RollingFileAppender 状态:

To be of any use, a RollingFileAppender must have both a RollingPolicy and a TriggeringPolicy set up. However, if its RollingPolicy also implements the TriggeringPolicy interface, then only the former needs to be specified explicitly.

因此,您可以 (1) 添加 <rollingPolicy>元素添加到您的 appender 配置,或 (2) 同时实现 RollingPolicy TriggeringPolicy 接口(interface),然后将该实现指定为 <rollingPolicy class='com.example.MyRollingPolicy'> 中的类(在这种情况下,触发策略是不必要的,因为 MyRollingPolicy 本身会处理它)。

关于android - 登录(安卓): SizeBasedTriggeringPolicy not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com