Log4j2的RoutingAppender的通配符模式 [英] Wildcard pattern for RoutingAppender of Log4j2

查看:96
本文介绍了Log4j2的RoutingAppender的通配符模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Log4j2的新RoutingAppender路由基于MDC(Log4j2中的ThreadContext)的不同日志.我想做的是以下事情:

I am trying to use the new RoutingAppender of Log4j2 to route the different logs based on the MDC (ThreadContext in Log4j2). What I want to do is the following:

  • 如果MDC映射具有$ contextId->追加到$ contextId附加器(特定日志)
  • 如果MDC没有$ contextId->附加到主附加程序(常规日志)

我想使用标记中的通配符模式来实现此目的,然后使用for contextId($ {ctx:contextId})中的键参数和主附加器使用默认值(不带键​​参数)进行过滤,但是我不知道那个通配符是哪个值.

I want to achieve this using a wildcard pattern in the tag and then filter using the key parameter in the for contextId (${ctx:contextId}) and using the default (without key paramenter) for the main appender, however I don't know which value is that wildcard.

我们将不胜感激,也许我正在从错误的道路上寻求帮助.我一直在阅读有关过滤器的信息,但似乎无法按我的意愿工作.

Any help is appreciated, maybe I am approaching this from the wrong path. I have been reading about Filters but don't seem to work as I want.

谢谢!

推荐答案

感谢链接Remko,我找到了一个临时解决方案,直到Log4j2的人员对该功能进行了改进.该解决方案同时使用RoutingAppender和Filters.这就是我的log4j2配置的样子(我定义了属性,但未在此处显示):

Thanks for the link Remko, I have found a temporary solution until that feature gets improved from the guys of Log4j2. The solution is using both RoutingAppender and Filters. This is how my log4j2 config looks like (I have properties defined but I am not showing them here):

<appenders>
    <appender name="applicationAppender" type="RollingFile" fileName="${logFileName}" filePattern="${logFileNamePattern}" bufferedIO="true" immediateFlush="true" append="true">
        <layout type="PatternLayout" pattern="${logPattern}" />
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="${logFileSize}" />
        </Policies>
        <DefaultRolloverStrategy max="${logFileCount}" />
    </appender>

    <Routing name="contextSpecificAppender">
        <Routes pattern="$${ctx:contextId}">
            <Route>
                <appender name="Rolling-${ctx:contextId}" type="RollingFile" fileName="logs/${ctx:contextId}.log" filePattern="${logFileNamePattern}" bufferedIO="true" immediateFlush="true" append="true">
                    <layout type="PatternLayout" pattern="${logPattern}" />
                    <Policies>
                        <TimeBasedTriggeringPolicy />
                        <SizeBasedTriggeringPolicy size="${logFileSize}" />
                    </Policies>
                    <DefaultRolloverStrategy max="${logFileCount}" />
                </appender>
            </Route>
        </Routes>
    </Routing>
</appenders>

<loggers>
    <root level="info">
        <appender-ref ref="contextSpecificAppender">
            <ThreadContextMapFilter onMatch="DENY" onMismatch="ACCEPT">
                <KeyValuePair key="contextId" value="" />
            </ThreadContextMapFilter>
        </appender-ref>
        <appender-ref ref="applicationAppender">
            <ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY">
                <KeyValuePair key="contextId" value="" />
            </ThreadContextMapFilter>
        </appender-ref>
    </root>
</loggers>

我要执行的操作是根据要记录的追加程序调用ThreadContext.put("contextId",")或ThreadContext.put("contextId","something"). 我希望可以迅速实现荒野功能,但与此同时,此解决方案对我来说已经足够.

What I do it is calling ThreadContext.put("contextId", "") or ThreadContext.put("contextId", "something") depending on what appender I want to log. I hope the wildward feature gets implemented soon, but for the meantime, this solution is enough for me.

谢谢!

这篇关于Log4j2的RoutingAppender的通配符模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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