简体   繁体   English

在每个用户的日志文件中使用特定标签进行记录

[英]Logging with specific label in log file for every user

I have web application in Java (JSP + Servlets) and logging with log4j 1.2.17. 我有Java(JSP + Servlets)Web应用程序,并使用log4j 1.2.17进行日志记录。 When user login on site he take ID. 用户登录网站时,他将获得ID。 I wanna see all his actions in log file with ID prefix like this: 我想在带有ID前缀的日志文件中看到他的所有操作,如下所示:

01.05.2015 11:12:30 INFO [Roy] - login correct

But i don't want to pass ID in every calling method to logging events in that method. 但是我不想将每个调用方法中的ID传递给该方法中的日志事件。 I want to save ID somewhere and take it when need. 我想将ID保存在某个地方,并在需要时使用它。

What you probably want is a Mapped Diagnostic Context . 您可能想要的是映射的诊断上下文 This effectively gives you a stack of ThreadLocal to put data elements into that you can log later. 这有效地为您提供了ThreadLocal堆栈,以将数据元素放入其中,以便您以后进行记录。

In java... 在Java中...

MDC.put('LOGIN_ID', 'Roy');

In your log pattern: 在您的日志模式中:

"%d %p [%X{LOGIN_ID}] %m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM