本文介绍了在Web应用程序中记录用户活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我希望能够在网络应用中记录用户活动。我目前正在使用log4j,它适用于记录错误等,但我不确定最好的方法是记录用户,执行的servlet方法和方法参数。我正在使用spring security进行身份验证。I'd like to be able to log user activities in a web app. I'm currently using log4j which works well for logging errors etc, but I'm unsure what the best approach is to log the user, executed servlet method, and method params. I'm using spring security for the authentication.典型的servlet可能如下所示:A typical servlet could look like:public class BankAccountServlet { @RequestMapping("/deposit") public void deposit(double amount) { ... } @RequestMapping("/checkBalance") public double checkBalance() { ... }}如果有两个用户,foo和bar,foo检查他的余额和酒吧存款两笔现金10.00和5.00。我希望日志看起来像:If there are two users, foo and bar, where foo checks his balance and bar deposits two sums of cash 10.00 and 5.00. I'd like the logs to look like:01/01/1970 23:59:59 - foo - checkBalance02/01/1970 23:59:59 - bar - deposit - 10.0002/01/1970 23:59:59 - bar - deposit - 5.00如果有人可以提供一些建议,我真的很感激他们的帮助。If anyone could offer some advice I'd really appreciate their help.推荐答案使用内置于Log4J中的MDC / NDC功能实现非常简单(SLF4J和Logback仅支持MDC)。It's actually pretty simple to achieve using MDC/NDC functionality built into Log4J (SLF4J and Logback only support MDC).首先,实现一个将用户名添加到MDC / NDC的servlet过滤器。 Logback提供了方便的 MDCInsertingServletFilter ,Spring框架也添加了 Log4jNestedDiagnosticContextFilter 到商店。看看它们但你需要一个这样的自定义:First, implement a servlet filter that will add username to MDC/NDC. Logback provides convenient MDCInsertingServletFilter, Spring framework also adds Log4jNestedDiagnosticContextFilter to the store. Look at them but you will need a custom one like this:public class UserToMdcFilter implements javax.servlet.Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { MDC.put("user", SecurityContextHolder.getContext().getAuthentication().getPrincipal()); try { chain.doFilter(request, response); } finally { MDC.remove("user"); } } //...} 将MDC值添加到日志记录模式 确保此过滤器应用于 web.xml Spring安全过滤器之后。 MDC功能非常灵活 - 如果需要,它会将MDC线程局部映射中保存的所有值添加到每个日志记录语句中。在您的情况下,只需添加以下内容:Adding MDC value to your logging patternMake sure this filter is applied in web.xml after Spring security filter. MDC feature is really slick - it will add all values saved in MDC thread-local map to each and every logging statement if requested. In your case, simply add this:%X{user}记录模式。记录方法名称,参数和返回值取决于您(用户名将自动添加),但有一些优雅的方法可以完全删除样板记录代码。试试这个Spring内置方面:Logging method name, parameters and return values is up to you (username will be added automatically), but there are some elegant ways to remove boilerplate logging code completely. Try this Spring built-in aspect:<bean id="customizableTraceInterceptor" class="org.springframework.aop.interceptor.CustomizableTraceInterceptor"> <property name="enterMessage" value="Entering $[methodName]($[arguments])"/> <property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/></bean><aop:config> <aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * BankAccountServlet.*(..))"/></aop:config> 最后的想法 看看这个帖子: http://forum.springsource.org/showthread.php?88890-MDC-Log4j-Filter-with-Spring-Security-3.0.2 考虑使用 Logback 作为日志库并坚持使用SLF4J API。 Final thoughtsLook at this thread: http://forum.springsource.org/showthread.php?88890-MDC-Log4j-Filter-with-Spring-Security-3.0.2Consider using Logback as a logging library and stick with SLF4J API. 这篇关于在Web应用程序中记录用户活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 16:02