java 您如何区分日志文件中的 log4j 会话与同一 Web 应用程序的副本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2763925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:42:46  来源:igfitidea点击:

How do you differentiate log4j sessions in a log file from copies of the same web-app?

javalogginglog4j

提问by EugeneP

There is only one file. And it is written simultaneously as web app copies run.

只有一个文件。它是在 Web 应用程序副本运行时同时编写的。

How do you filter only one session log messages from other log lines?

如何从其他日志行中仅过滤一个会话日志消息?

回答by John D

Using a servlet filter with either NDC or MDC information is the best way I've seen. A quick comparison of the two is available at http://wiki.apache.org/logging-log4j/NDCvsMDC.

使用带有 NDC 或 MDC 信息的 servlet 过滤器是我见过的最好方法。在http://wiki.apache.org/logging-log4j/NDCvsMDC上可以快速比较两者。

I've found MDC has worked better for me in the past. Remember that you'll need to update your log4j properties file to include whichever version you prefer (pattern definitions at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html).

我发现 MDC 过去对我来说效果更好。请记住,您需要更新 log4j 属性文件以包含您喜欢的任何版本(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html 上的模式定义)。

A full example of configuring MDC with a servlet filter is available at http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/.

使用 servlet 过滤器配置 MDC 的完整示例可在http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/ 获得

A slightly easier to configure, but significantly inferior option: You could opt to just print out the thread ID (via the properties file) for each request and make sure that the first thing you log about each request is a session identifier. It isn't as proper (or useful), but it can work for low-volume applications.

一个稍微容易配置但明显较差的选项:您可以选择只打印每个请求的线程 ID(通过属性文件),并确保您记录的关于每个请求的第一件事是会话标识符。它不是那么合适(或有用),但它可以用于小批量应用程序。

回答by Péter T?r?k

You could set a context message including the identifier of the specific app instance using org.apache.log4j.NDC, like this:

您可以使用org.apache.log4j.NDC设置包含特定应用程序实例标识符的上下文消息,如下所示:

String appInstanceId = "My App Instance 1";
org.apache.log4j.NDC.push(appInstanceId);
// handle request
org.apache.log4j.NDC.clear();

You can set up the context during the initialization of your web app instance, or inside the doPost()method of your servlets. As its name implies, you can also nest contexts within contexts with multiple pushcalls at different levels.

您可以在 Web 应用程序实例初始化期间或在doPost()servlet的方法内设置上下文。顾名思义,您还可以在具有push不同级别的多个调用的上下文中嵌套上下文。

See the section "Nested Diagnostic Contexts" in the Log4J manual.

请参阅Log4J 手册中的“嵌套诊断上下文”部分。

回答by Alexander Pogrebnyak

Here is a page that sets up an MDC filter for web-app -> http://rtner.de/software/MDCUserServletFilter.html

这是一个为 web-app 设置 MDC 过滤器的页面 -> http://rtner.de/software/MDCUserServletFilter.html

Being a servlet filter it will free you from managing MDC/NDC in each of your servlets.

作为一个 servlet 过滤器,它将使您无需在每个 servlet 中管理 MDC/NDC。

Of course, you should modify it to save information more pertinent to your web-app.

当然,您应该修改它以保存与您的网络应用程序更相关的信息。

回答by Dima

If you want to differentiate sessions in the same application then the MDC is the way to go. But if you want to differentiate the web applications writing to the same file, then MDC won't help because it works on a thread basis. In such case I used to make my own appender which knows which application instance it serves. This can be done through appender configuration properties. Such appender would stick application name into each logging event as a property before writing it into the media, and then you can use a layout to show this property value in the text file it writes to. Using MDC in such case won't work because every thread will have to MDC.put(applicationName) and that is quite ugly. MDC is only good for single process, not for several processes. If someone knows the other way, I'd like to hear.

如果您想区分同一应用程序中的会话,那么 MDC 是您的最佳选择。但是,如果您想区分写入同一文件的 Web 应用程序,那么 MDC 将无济于事,因为它以线程为基础工作。在这种情况下,我曾经制作自己的 appender,它知道它服务于哪个应用程序实例。这可以通过 appender 配置属性来完成。在将应用程序名称写入媒体之前,此类 appender 会将应用程序名称作为属性粘贴到每个日志记录事件中,然后您可以使用布局在它写入的文本文件中显示此属性值。在这种情况下使用 MDC 是行不通的,因为每个线程都必须使用 MDC.put(applicationName),这非常难看。MDC 只适用于单个进程,不适用于多个进程。如果有人知道另一种方式,我想听听。