log4j 和 java.util.logging 有什么区别

发布于 2024-10-08 21:16:32 字数 50 浏览 0 评论 0原文

哪个最适合获取登录其帐户的用户的日志文件?用一个小例子解释一下...谢谢您的时间...

Which is best for getting the log files of user logged in his account? Explain with a small example... Thanks for your time...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-10-15 21:16:32

Log4j 是 Java 的事实上的标准日志库。

Java.util.logging 是 Java 中内置的日志记录机制,但这并不意味着它是最好的...

使用 Log4j 及其 MDC。这样,您可以轻松地记录不同的用户帐户,如下所示:

MDC.put(user);
logger.log("Deleted something important!");

这样,如果日志记录配置正确,您将在日志输出中看到如下内容:

[用户 Alice] 删除了重要的内容!

这适用于多用户环境中的每个用户。

注意:如果您要开始一个新项目,我建议使用 slf4j登录。这种组合比 log4j 或 java.util.logging 更强大。

我已经使用这个组合有一段时间了,它确实得到了丰厚的回报。除其他外,对于消除错误和审核用户交互非常有用。

Log4j is a defacto standard logging library for Java.

Java.util.logging is a built in logging mechanism in Java, but that doesn't make it the greatest...

Use Log4j and its MDC. This way, you can easily log different user accounts like this:

MDC.put(user);
logger.log("Deleted something important!");

This way, if logging is configured correctly, in your log output you will see something like this:

[user Alice] Deleted something important!

and this will work for every user in a multi user environment.

Note: if you're starting a new project, I'd suggest using slf4j and Logback. This combination is even more powerful than log4j or java.util.logging.

I have been using this combination for some time and it has really paid off handsomely. Very useful for bug squashing and auditing user interaction, among other things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文