手动登录回滚 [英] logback manually call rollover
问题描述
使用logback我想在每次异步作业启动时启动新日志,因此我需要手动调用翻转.但是,当我尝试获取appender时,我得到的是null.下面是我的配置:
Using logback I would like to start new log every time an async job starts so I need to call rollover manually. But when I try to get appender I get null instead. Below is my config:
<configuration scan="true">
<timestamp key="time" datePattern="yyyy-MM-dd_HH_mm"/>
<logger name="com.my.com.pany" level="DEBUG">
<appender name="TEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/log_TEST_${time}.log</file>
<triggeringPolicy
class="com.my.com.pany.myapp.logging.ManualRollingPolicy">
</triggeringPolicy>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</logger>
</configuration>
我这样称呼过渡:
ch.qos.logback.classic.Logger logF = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.my.com.pany");
RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) logF.getAppender("test");
appender.rollover();
我扩展了TimeBasedRollingPolicy<E>
,以便当我触发异步作业时我的日志将开始:
I extended TimeBasedRollingPolicy<E>
so that my log will start when i trigger async job:
@NoAutoStart
public class ManualRollingPolicy<E> extends TimeBasedRollingPolicy<E> {
}
有人可以帮我解决这个问题吗?
经过进一步的调查,我可以看到LogF
的大小为appenderList
的appenderList
正确设置了我的自定义RollingPolicy
.但是,此追加器的name
属性设置为null,我认为这就是为什么无法按名称获取它的原因.
Could somone help me with this issue?
Upon some further investigation I can see that LogF
has appenderList
of size 1 which has my custom RollingPolicy
properly set. However name
property of this appender is set to null and I think that is the reason why I can't get it by name.
推荐答案
所以我得到了一个非常简洁的解决方法,我认为有人可能会有所帮助.您可以使用 SiftingAppender . 顾名思义,SiftingAppender可用于根据给定的运行时属性来分离(或筛选)日志记录"
So I got a pretty neat workaround which I think someone may find helpful. You can use SiftingAppender. "As its name implies, a SiftingAppender can be used to separate (or sift) logging according to a given runtime attribute"
因此,在我以批处理作业开始的日期为例,您将附加器配置为采用一些唯一的参数:
So you configure your appender to take some unique parameter, in my case date of batch job start:
<appender name="FULL" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>id</key>
<defaultValue>000000</defaultValue>
</discriminator>
<sift>
<appender name="FULL-${id}" class="ch.qos.logback.core.FileAppender">
<file>logs/log_${id}.log</file>
<append>false</append>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>
然后您手动调用:
MDC.put("id",id);
为了开始新的日志,当您完成这个特殊的日志文件后,您只需记录一个FINALIZE_SESSION_MARKER
常量:
logger.info(ClassicConstants.FINALIZE_SESSION_MARKER)
.在我看来,它足够灵活,可以回答手动翻转的问题.
And then you manually call:
MDC.put("id",id);
in order to start new log and when you are done with this paticular log file you just log a FINALIZE_SESSION_MARKER
constant:
logger.info(ClassicConstants.FINALIZE_SESSION_MARKER)
. In my opinion it is flexible enough that it answers the question of manuall rollover.
这篇关于手动登录回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!