在log4j中使用MDC动态命名日志文件 [英] Using MDC in log4j to dynamically name the log file

查看:172
本文介绍了在log4j中使用MDC动态命名日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在运行时使用MDC命名日志文件。

Is it possible some how to use MDC to name the log file at run time.

我有一个Web应用程序,使用tomcat docbase同时使用不同的名称调用。所以我需要为每个文件都有单独的日志文件。

I have a single web application which is being called by different names at the same time using tomcat docbase. So i need to have separate log files for each of them.

推荐答案

这可以通过 Logback ,Log4J的后续版本。

This can be accomplished in Logback, the successor to Log4J.


Logback旨在作为流行的log4j项目的后续版本,从而恢复log4j离开的位置。 / p>

Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.

请参阅的文档筛选Appender


SiftingAppender在引用和配置嵌套式appender方面具有独特性。在上面的示例中,在SiftingAppender中将有嵌套的FileAppender实例,每个实例由与useridMDC键关联的值标识。每当为useridMDC键分配新值时,将从头开始构建新的FileAppender实例。 SiftingAppender跟踪它创建的appender。未使用30分钟的Appenders将自动关闭并丢弃。

The SiftingAppender is unique in its capacity to reference and configure nested appenders. In the above example, within the SiftingAppender there will be nested FileAppender instances, each instance identified by the value associated with the "userid" MDC key. Whenever the "userid" MDC key is assigned a new value, a new FileAppender instance will be built from scratch. The SiftingAppender keeps track of the appenders it creates. Appenders unused for 30 minutes will be automatically closed and discarded.

在该示例中,它们基于MDC值为每个用户生成单独的日志文件。
根据您的需要,可以使用其他MDC值。

In the example, they generate a separate log file for each user based on an MDC value. Other MDC values could be used depending on your needs.

这篇关于在log4j中使用MDC动态命名日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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