简体   繁体   English

在网络应用中记录当前用户

[英]Log current user in web app

I want to log the current user of my web app. 我想登录我的Web应用程序的当前用户。 I tried this with an own implementation of the StrLookup interface where i call something like Environment.getCurrentUser(), but the lookup is executed just once during the initiatization of the logger. 我使用自己的StrLookup接口实现尝试了此操作,在其中我调用了Environment.getCurrentUser()之类的东西,但是在记录器初始化期间仅执行了一次查找。 Is there a thread specific lookup functionality, that will be executed every time, like the keywords in patterns? 是否有特定于线程的查找功能,每次都会执行,例如模式中的关键字?

What I do is use a servlet filter or a Spring Interceptor and add items to the Log4j2 ThreadContext before the request and then remove them after the request. 我要做的是使用Servlet过滤器或Spring Interceptor,在请求之前将项目添加到Log4j2 ThreadContext中,然后在请求之后将其删除。 This will cause the information to be included in every logging event and can be referenced in the pattern layout via the %X converter. 这将使该信息包含在每个记录事件中,并且可以通过%X转换器在模式布局中引用该信息。

See http://logging.apache.org/log4j/2.x/manual/eventlogging.html for an example. 有关示例,请参见http://logging.apache.org/log4j/2.x/manual/eventlogging.html

This can be useful if you want to write an application that has to be sensitive to the customer or client involved but you don't want to have to pass a customer or client id through every method. 如果您要编写对所涉及的客户或客户敏感的应用程序,但又不想通过每种方法传递客户或客户ID,则此功能很有用。

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

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