是否有可能一些如何使用 MDC 在运行时命名日志文件。
我有一个 Web 应用程序,它使用 tomcat 文档库同时被不同的名称调用。所以我需要为他们每个人准备单独的日志文件。
最佳答案
这可以在 Logback 中完成,Log4J 的继任者。
Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.
请参阅 Sifting Appender 的文档
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 值。
关于java - 在log4j中使用MDC动态命名日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7992473/