我尝试在JDK1.6应用程序上使用Log4j 1.2.17,根据日志中的模式将特定日志路由到不同的文件。我发现在这里的帖子中提到的解决方案,这里说在log4j xml中完成的以下配置应该与MDC上下文沿着工作,但由于某种原因,它们对过滤器没有影响,日志最终被路由到控制台和文件。
我的java代码看起来如下:
@RequestMapping(value = "/fetchBookById", method = RequestMethod.GET, produces="application/json")
public @ResponseBody Map<String,? extends Object> bookApi(ParamDTO params) throws InterruptedException {
log.info("SimpleBookController.bookApi: THIS IS A COMMON LOG");
Map<String,Object> rxdMap = null;
try{
//This below will put either ABCD or EFGH etc
MDC.put("COMPANYNAME", companyMap.get(params.getCompanyOwnerId()));
log.info("SimpleBookController.bookApi: THis is test with routing");
log.info("SimpleBookController.bookApi: Payload: "+params.toString());
//do something here
log.info("SimpleBookController.bookApi API completed for :"+params.getCompanyOwnerId());
} finally {
MDC.clear();
}
return rxdMap;
}
Log4j.xml:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] COMPANYNAME:%X{COMPANYNAME} %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.StringMatchFilter">
<param name="StringToMatch" value="COMPANYNAME:"/>
<param name="AcceptOnMatch" value="false"/>
</filter>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="./logs/company.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="10MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] COMPANYNAME:%X{COMPANYNAME} %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.StringMatchFilter">
<param name="StringToMatch" value="COMPANYNAME:ABCD"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<root>
<level value="info" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
在上面的配置中,我试图将没有“COMPANYNAME:“字符串的日志路由到控制台,而具有“COMPANYNAME:ABCD”字符串的日志应该路由到特定的文件。但是控制台和文件Appender最终都拥有所有的日志,而不考虑字符串模式。如果有人能给我指出正确的方向,我将不胜感激。
示例日志:
2022-07-11 21:29:44 [[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] COMPANYNAME: INFO - FrameworkServlet 'TestJDK6Logging': initialization completed in 423 ms
2022-07-11 21:29:56 [[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] COMPANYNAME: INFO - SimpleBookController.bookApi: THIS IS A COMMON LOG
2022-07-11 21:29:56 [[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] COMPANYNAME:ABCD INFO - SimpleBookController.bookApi: THis is test with routing
2022-07-11 21:29:56 [[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] COMPANYNAME:ABCD INFO - SimpleBookController.bookApi: Payload: ParamDTO [identifier=sdfdf, bookId=1, companyOwnerId=4]
2022-07-11 21:30:01 [[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'] COMPANYNAME:ABCD INFO - SimpleBookController.bookApi API completed for :4
1条答案
按热度按时间rxztt3cl1#
最后我创建了一个自定义的控制台和日志过滤器,根据MDCMap值来转移日志流,这样就解决了过滤问题。
我的log4j.xml如下所示:
我的自定义过滤器Log4JConsoleFilter和Log4JCompanyFilter如下所示:
}
}