使用 Log4j 的每个用户都有不同的日志
我有一个网络应用程序,我想为每个用户使用不同的日志,这样我就可以获得用户在系统上执行的操作的“历史记录”。
这就是我到目前为止所遇到的:
import java.io.File;
import java.io.IOException;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.Logger;
public class LogManager {
public Logger getLog(String username) throws IOException{
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new DailyRollingFileAppender(layout, "users"+File.pathSeparator+username+File.pathSeparator+username, "'.'yyyy-MM");
// configure the appender here, with file location, etc
appender.activateOptions();
Logger logger = Logger.getRootLogger();
logger.addAppender(appender);
return logger;
}
}
问题是,作为一个网络应用程序,是多线程的,所以据我所知,我不能一直使用 RootLogger
并根据我所在的用户更改附加程序记录。我认为我应该为每个用户创建不同的 Logger
,但这是正确的吗?
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:
import java.io.File;
import java.io.IOException;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.Logger;
public class LogManager {
public Logger getLog(String username) throws IOException{
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new DailyRollingFileAppender(layout, "users"+File.pathSeparator+username+File.pathSeparator+username, "'.'yyyy-MM");
// configure the appender here, with file location, etc
appender.activateOptions();
Logger logger = Logger.getRootLogger();
logger.addAppender(appender);
return logger;
}
}
The problem is that, as a webapplication, is multithreaded, so AFAIK I can't use RootLogger
all the time and change the appenders depending on the user who I'm logging. I think I should create different Logger
for each user, but is that correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试切换到 logback (log4j 的后继者)。它带有一个 SiftingAppender ,可用于根据给定的运行时属性,在您的情况下为“userid”。 文档包含基于用户 ID 分离日志的示例。
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.
我建议使用日志上下文信息来记录用户的任何特定操作,并将其包含在日志记录中。
然后,每当您需要特定用户的日志时,就可以搜索单个日志文件。如果需要拆分所有文件,请在日志轮换时进行。这种后处理比同时为每个用户保留一个打开的文件要简单得多。
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.
“嵌套诊断上下文”适用于这样的用例 - 您可以使用 ID 来标记每个日志语句以识别用户(例如 IP 地址、用户名等),
更多信息请参见:http://logging.apache.org/log4j/1.2/apidocs /org/apache/log4j/NDC.html
(编辑:这里是关于 NDC 的另一篇有用的文章:何时使用“嵌套诊断”上下文'(NDC)?)
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)? )