问题描述
只有一个文件.它是在Web应用程序副本运行时同时编写的.
如何仅过滤来自其他日志行的一个会话日志消息?
推荐答案
使用NDC或MDC信息的Servlet过滤器是我见过的最好的方法.可以在 http://wiki.apache.org.org/logge.org/logging-logging-log4j/获取快速比较. ndcvsmdc .
过去,我发现MDC过去对我的效果更好.请记住,您需要更新log4j属性文件以包括您喜欢的任何版本( http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diarostic-diarostic-context-context-example-code/.
一个易于配置的略易于配置,但是明显的下方面的选择要较低:您可以选择为每个请求打印出线程ID(通过属性文件),并确保您登录的每个请求的第一件事是会话标识符.它不是正确的(或有用),但可以适用于小批量应用程序.
其他推荐答案
您可以使用 org.apache.log4j.ndc ,像这样:
String appInstanceId = "My App Instance 1"; org.apache.log4j.NDC.push(appInstanceId); // handle request org.apache.log4j.NDC.clear();
您可以在Web应用程序实例的初始化或servlet的doPost()内部设置上下文.顾名思义,您还可以在不同级别的多个push调用的上下文中嵌套上下文.
请参阅 log4j手册./p>
其他推荐答案
这是一个页面,该页面为Web -App-> http:http:http:http://rtner.de/software/mdcuserservletfilter.html
作为servlet过滤器,它将释放您在每个servlet中管理MDC/NDC.
当然,您应该对其进行修改,以节省与您的Web应用程序更相关的信息.
问题描述
There is only one file. And it is written simultaneously as web app copies run.
How do you filter only one session log messages from other log lines?
推荐答案
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.
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).
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/.
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.
其他推荐答案
You could set a context message including the identifier of the specific app instance using org.apache.log4j.NDC, like this:
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 push calls at different levels.
See the section "Nested Diagnostic Contexts" in the Log4J manual.
其他推荐答案
Here is a page that sets up an MDC filter for web-app -> http://rtner.de/software/MDCUserServletFilter.html
Being a servlet filter it will free you from managing MDC/NDC in each of your servlets.
Of course, you should modify it to save information more pertinent to your web-app.