Log4J2 appender没有登录到ThreadContext文件夹 [英] Log4J2 appender not logging to ThreadContext folder

查看:96
本文介绍了Log4J2 appender没有登录到ThreadContext文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Log4J2配置XML:

 <路由名=myAppender> 
< Routes pattern =$$ {ctx:workId}>
< Route>
<文件fileName =$ {my-path} / sites / $ {ctx:workId} / $ {date:yyyy-MM-dd} / $ {ctx:employeeId} /emp.logname = myAppender - $ {CTX:workId} >
< MarkerFilter marker =TELEMETRIConMatch =ACCEPTonMismatch =DENY/>
< PatternLayout>
<模式> [%date {ISO8601}] [% - 5level] [%logger {1。}] [%marker] [$$ {ctx:employeeId}]%X%m%n< / Pattern> ;
< / PatternLayout>
< / File>
< / Route>
< / Routes>
< / Routing>

< Async name =Async>
< AppenderRef ref =myAppenderlevel =info/>
< / Async>

然而,只要log4j2 appender写入文件,它就不会使用正确的 $ {ctx:employeeId} 写入指定文件路径时的字符串。



log4j2 appender第一次写入文件,它写入 $ {ctx:employeeId} 指定的正确文件路径。但是每当有关具有另一个Id的员工的信息被放入线程上下文时,appender仍然会记录到旧的文件路径。



例如第一次appender记录它写到正确的路径:

  D:/ example / logs / sites / 1 / 2015-08-22 / 2 / emp.log 

但是当记录器记录下一个雇员Id时(使用新的ThreadContext)它仍在登录

  D:/ example / logs / sites / 1 / 2015-08-22 / 2 / emp。 log 

而不是例如:

  D:/example/logs/sites/1/2015-08-22/3/emp.log 

(注意员工ID不同。)



在日志输出模式中,我记录当前的employeeId [$$ {ctx:employeeId}] ,以及当前线程上下文%X 中的内容。输出显示正在使用正确的employeeId并且在ThreadContext中,但是appender没有记录到 $ {ctx:employeeId} 文件路径。



有人知道我遗失了什么吗?或者如果我做错了什么?或者这可能是Log4j2的错误?感谢您的帮助!

解决方案

嗯,我知道这个答案在提出问题后很久就会出现,但如果这可能仍然存在帮助你或其他人我会分享我发现的东西。



首先,我无法重现问题,但我确实有代码可以实现你想要的东西,所以我希望能够分享我的代码通过观察它你可以确定你哪里出错了。如果没有,请提供



第一个日志包含:

  [2017-04-13T20:42:49,814] [INFO] [p.Log4j2DiffFilePerCtxVarMain] [TELEMETRIC] [mainEmployeeId] {employeeId = mainEmployeeId,workId = mainWorkId嘿,这里是来自main的一些信息日志! 

第二个日志包含:

  [2017-04-13T20:42:49,822] [INFO] [p.Log4j2DiffFilePerCtxVarMain $ 1] [TELEMETRIC] [thread1EmployeeId] {employeeId = thread1EmployeeId,workId = thread1WorkId}嘿这里是一些信息日志来自线程1! 

希望这有帮助!


I have the following Log4J2 configuration XML:

<Routing name="myAppender">
    <Routes pattern="$${ctx:workId}">
        <Route>
            <File fileName="${my-path}/sites/${ctx:workId}/${date:yyyy-MM-dd}/${ctx:employeeId}/emp.log" name="myAppender-${ctx:workId}">
                <MarkerFilter marker="TELEMETRIC" onMatch="ACCEPT" onMismatch="DENY"/>
                <PatternLayout>
                    <Pattern>[%date{ISO8601}][%-5level][%logger{1.}][%marker][$${ctx:employeeId}] %X%m%n</Pattern>
                </PatternLayout>
            </File>
        </Route>
    </Routes>
</Routing>

<Async name="Async">     
    <AppenderRef ref="myAppender" level="info"/>
</Async>

However whenever the log4j2 appender writes to a file, it doesn't use the proper ${ctx:employeeId} string when it writes to the specified file path.

The first time the log4j2 appender writes to a file, it writes to the correct file path specified by ${ctx:employeeId}. But whenever information about an employee with another Id is put into the thread context, the appender is still logging to the old file path.

For example the first time the appender logs it writes to the correct path:

D:/example/logs/sites/1/2015-08-22/2/emp.log

But when the the logger is logging for the next employee Id (With a new ThreadContext) it is still logging into

D:/example/logs/sites/1/2015-08-22/2/emp.log

instead of for example:

D:/example/logs/sites/1/2015-08-22/3/emp.log

(Note the employee Id is different.)

In the log output pattern, I log the current employeeId [$${ctx:employeeId}], as well as what is in the current thread context %X. The output shows that the correct employeeId is being used and is in the ThreadContext, but the appender is not logging to that ${ctx:employeeId} file path.

Does anyone know if I am missing anything? Or if I am doing something wrong? Or is this possibly a bug with Log4j2? Thanks for any help!

解决方案

Well, I realize this answer comes long after the question has been asked but in case this might still help you or someone else I'll share what I've found.

First of all I'm not able to reproduce the problem, but I do have code that achieves what you want so I'm going to share my code in the hope that by looking at it you can determine where you went wrong. If not then please provide a Minimal, Complete, and Verifiable Example so that anyone trying to help you can reproduce the issue.

Here is my working code:

package pkg;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;

public class Log4j2DiffFilePerCtxVarMain {
    private static final Marker TELEMETRIC = MarkerManager.getMarker("TELEMETRIC");

    public static void main(String[] args){
        Logger log = LogManager.getLogger();

        ThreadContext.put("workId", "mainWorkId");
        ThreadContext.put("employeeId", "mainEmployeeId");

        log.info(TELEMETRIC, "Hey here's some info log from main!");

        Thread t1 = new Thread(new Runnable(){
            public void run(){
                Logger log = LogManager.getLogger();
                ThreadContext.put("workId", "thread1WorkId");
                ThreadContext.put("employeeId", "thread1EmployeeId");

                log.info(TELEMETRIC, "Hey here's some info log from thread1!");
            }
        });
        t1.start();
    }
}

Here is the log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Routing name="myAppender">
            <Routes pattern="$${ctx:workId}">
                <Route>
                    <File
                        fileName="logs/${ctx:workId}/${date:yyyy-MM-dd}/${ctx:employeeId}/emp.log"
                        name="myAppender-${ctx:workId}">
                        <MarkerFilter marker="TELEMETRIC" onMatch="ACCEPT"
                            onMismatch="DENY" />
                        <PatternLayout>
                            <Pattern>[%date{ISO8601}][%-5level][%logger{1.}][%marker][$${ctx:employeeId}] %X %m%n</Pattern>
                        </PatternLayout>
                    </File>
                </Route>
            </Routes>
        </Routing>

        <Async name="Async">
            <AppenderRef ref="myAppender" level="info" />
        </Async>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <appender-ref ref="Async" />
        </Root>
    </Loggers>
</Configuration>

When I run this code with this log4j2.xml I see the following results:

The first log contains this:

[2017-04-13T20:42:49,814][INFO ][p.Log4j2DiffFilePerCtxVarMain][TELEMETRIC][mainEmployeeId] {employeeId=mainEmployeeId, workId=mainWorkId} Hey here's some info log from main!

The second log contains this:

[2017-04-13T20:42:49,822][INFO ][p.Log4j2DiffFilePerCtxVarMain$1][TELEMETRIC][thread1EmployeeId] {employeeId=thread1EmployeeId, workId=thread1WorkId} Hey here's some info log from thread1!

Hope this helps!

这篇关于Log4J2 appender没有登录到ThreadContext文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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