初学者的 log4J 问题

发布于 2024-09-03 13:57:43 字数 218 浏览 1 评论 0原文

我使用转换模式配置了 log4j 以用于基本目的:-

log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n

但现在我想记录发生错误的类名以及用户名(在会话对象中可用)以及该事件发生时的日期和时间。我该怎么做?我需要对格式字符串进行哪些更改?

提前致谢 :)

i configured log4j for basic purpose usin the conversion pattern :-

log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n

But now i want to log the class name from which the error came as well as the username(available in session object) as well as the date and time when that event occurs. How do i do this? What changes do i need to make in format string?

Thanks in advance :)

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

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

发布评论

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

评论(2

请恋爱 2024-09-10 13:57:43

查看 PatternLayout 文档大部分都是你想要的。

您面临的头痛是从会话中获取用户名(Log4j 无法自动执行此操作)。我也许会调查 NDCMDC,并从会话中填充这些(也许在 servlet 过滤器中?)。它们是每个线程的,因此假设您的用户具有相同的范围,那么这可能会有所帮助。

Take a look at the PatternLayout docs for most of what you want.

The headache you face is getting the user name from the session (Log4j can't do this automatically). I would perhaps investigate NDCs or MDCs, and populate these from the session (perhaps in a servlet filter?). They are per-thread, so assuming your user has the same scope then this may help.

青衫负雪 2024-09-10 13:57:43

要获取类名,您可以使用 %l,但性能会受到一些影响。
要获取用户名,您需要使用映射或嵌套诊断上下文然后在模式字符串中分别指定 %X 或 %x。

检查 PatternLayout javdocs。

To get the class name out you can use %l, but you'll take a bit of performance hit.
To get the username out, you'll need to use a mapped or nested diagnostic context and then specificy %X or %x respectively in the pattern string.

Check the PatternLayout javdocs.

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