我正在尝试在某个包中为每个类设置单独的日志文件。我发现最接近的解决方案是使用RoutingAppender,并为调用getLogger方法时添加到ThreadContext的某些类创建一个特殊的getLogger。但是,此解决方案不会创建任何新的日志文件。我一直在阅读stackoverflow条目和log4j2文档,但无法理解我出错的地方。这是我查看的一些条目/文档
任何人都可以帮助我理解为什么我没有任何className日志文件吗?
这里是我的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>
这是我在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;
}
这是来自其中一个需要它自己的日志文件的类的调用:
private final static Logger log = myUtils.getLogger(OpenTest.class.getName());
答案 0 :(得分:0)
你能尝试在你已经拥有的Routes元素中添加另一个Route部分,key =“$$ {ctx:className}”?
<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>