简体   繁体   English

Log4j2使用类/记录器名称作为RoutingAppender的模式

[英]Log4j2 Using class/logger name as a pattern for RoutingAppender

I am trying to set up individual logging files per class in a certain package. 我试图在某个包中为每个类设置单独的日志文件。 The closest solution I've found is to use the RoutingAppender and to have a special getLogger for those certain classes that adds to a ThreadContext when that getLogger method is called. 我找到的最接近的解决方案是使用RoutingAppender并为那些特定类提供特殊的getLogger,该类在调用该getLogger方法时会添加到ThreadContext中。 However, this solution isn't creating any new log files. 但是,此解决方案不会创建任何新的日志文件。 I've been reading stackoverflow entries and log4j2 documentation, but can't understand where I'm going wrong. 我一直在阅读stackoverflow条目和log4j2文档,但无法理解我要去哪里。 Here's some of entries/documentation that I've looked at 这是我看过的一些条目/文档

Can anyone help me understand why I don't have any of the className log files? 谁能帮助我了解为什么我没有任何className日志文件?

Here's the relevant excepts from my log4j2.xml: 这是我的log4j2.xml中的相关例外:

    <Routing name="Routing">
        <Routes pattern="$${ctx:className}">
            <Route>
                <File name="testCaseLog"
                    fileName="${ctx:className}.log">
                    <PatternLayout
                        pattern="%d{HH:mm:ss} %-5p [%t] %c{1} - %m%n" />
                </File>
            </Route>
        </Routes>
    </Routing>
</Appenders>
<Loggers>
    (other loggers)
    <Root level="info">
        <AppenderRef ref="stdout" />
        <AppenderRef ref="Routing"/>
    </Root>
</Loggers>

Here's the bit from my logger function in a myUtils class 这是myUtils类中的记录器功能的部分内容

public static Logger getLogger(String className) {
    ThreadContext.put("className", className);

    //This is using the slf4j LoggerFactory. I'm starting to think this might be the problem, but I can't get away from slf4j
    Logger log = LoggerFactory.getLogger(className);

    return log;
}

Here's a call from within one of the classes that needs it's own log file: 这是来自需要它自己的日志文件的类之一的调用:

    private final static Logger log = myUtils.getLogger(OpenTest.class.getName());

Can you try adding another Route section to the Routes element you already have, with key="$${ctx:className}"? 您可以尝试使用key =“ $$ {ctx:className}”将另一个Route部分添加到已有的Routes元素中吗?

        <Route key="$${ctx:className}">
            <File name="testCaseLog"
                fileName="${ctx:className}.log">
                <PatternLayout
                    pattern="%d{HH:mm:ss} %-5p [%t] %c{1} - %m%n" />
            </File>
        </Route>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM