A different log for every user using Log4j

后端 未结 3 1096
轻奢々
轻奢々 2021-01-06 01:06

I have a webapplication and I want to use a different log for every user, so I can have a \"history\" of what the user did on the system.

This is what I have so far:

相关标签:
3条回答
  • 2021-01-06 01:53

    I would suggest using a log context information to record the user for any particular action, and include that in your log records.

    Then whenever you need the log for a particular user, trawl through the single log file. If you need all the files split, do it when the log rotates. This post-processing will be a lot simpler than keeping an open file for every user concurrently.

    0 讨论(0)
  • 2021-01-06 01:58

    Try switching to logback (log4j's successor). It comes with a SiftingAppender which can be used to separate (or sift) logging according to a given runtime attribute, which would be "userid" in your case. The documentation contains an example for separating logs based on userid.

    0 讨论(0)
  • 2021-01-06 02:08

    The "Nested Diagnostic Context" is meant for such a use case - you can stamp each log statement with an id to identity the user (like an IP address, username, etc)

    more here: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html

    (edit: here another useful post on NDC: When to use 'nested diagnostic context' (NDC)? )

    0 讨论(0)
提交回复
热议问题