您是否见过 Log4j 中基于 NDC 记录到单独文件的附加程序?

发布于 2024-07-20 02:12:30 字数 1542 浏览 4 评论 0原文

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

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

发布评论

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

评论(4

波浪屿的海角声 2024-07-27 02:12:30

Logback(log4j 的后继者)附带的 SiftingAppender 正是为了处理这种情况而设计的。

顾名思义,SiftingAppender 可用于根据给定的运行时属性来分离(或筛选)日志记录。 例如,SiftingAppender 可以根据用户会话分离日志记录事件,以便每个用户生成的日志进入不同的日志文件,每个用户一个日志文件。 例如,SiftingAppender 可以将日志记录事件分离到不同的日志文件中,每个用户一个文件。

SiftingAppender 文档包含一个根据用户 ID 分离日志的示例。

SiftingAppender which ships with logback (log4j's successor) is designed precisely to handle this situation.

As its name implies, a SiftingAppender can be used to separate (or sift) logging according to a given runtime attribute. For example, SiftingAppender can separate logging events according to user sessions, so that the logs generated by every user go into distinct log files, one log file per user. For example, SiftingAppender can separate logging events into distinct log files, one file per user.

The documentation for SiftingAppender contains an example for separating logs based on user id.

凉栀 2024-07-27 02:12:30

我不知道有任何标准甚至第三方附加程序可以做到这一点,虽然您是正确的,您可以编写自己的附加程序,但我倾向于只使用标准文件附加程序之一(滚动或其他方式)并使用一种工具可以过滤文本行(即从视图中排除与模式不匹配的任何行)以读取日志。 大多数可以过滤的编辑器也可以设置过滤器集,以便您可以重用现有模式。 我不会讨论使用哪个编辑器,因为我什至不知道您正在使用什么操作系统,而且每个人都有自己的偏好。

I am not aware of any standard or even 3rd party appenders that can do this and while you are correct that you could write your own I would be inclined to just use one of the standard file appenders (rolling or otherwise) and use a tool that can filter lines of text (i.e. exclude any lines not matching a pattern from view) to read the log. Most editors that can filter can also set up filter sets so you can reuse existing patterns. I won't discuss which editor to use as I don't even know what OS you're using and everyone has their own preferences.

无声无音无过去 2024-07-27 02:12:30

听起来你想要一个多文件附加器之类的东西,你给出文件名的一部分,其余部分用 NDC 填充。 我不知道有这样的事情。 也许你必须自己动手。

如果您自己推出,您可能希望创建一个在内部使用动态创建的 RollingFileAppenderMapAppender。 但是,我不知道 Appender 是否有权访问 NDC。 这可能是一项不平凡的任务。

It sounds like you want a multi-file appender sort of thing, where you give a part of a filename and the rest is filled in with the NDC. I am not aware of anything like this. Probably you would have to roll your own.

If you roll your own, you probably want to create an Appender that internally uses a Map of RollingFileAppenders that is created dynamically. I don't know if an Appender has access to the NDC, however. This would probably be a non-trivial undertaking.

风铃鹿 2024-07-27 02:12:30

我也从未见过这样的附加程序,但我认为编写自己的附加程序没什么大不了的。 如果你想自己做 - 查看 AppenderSkeleton< /a>,您至少需要覆盖append(..) 方法。 当你的appender被调用时,你将得到完整的事件,其中包含所需的一切,包括NDC。 然后做任何你喜欢的事情......我认为 MDC(映射诊断上下文)在你的情况下是可行的替代方案,恕我直言,它更灵活。

如果您正在寻找商业解决方案 - 请查看 logFaces,它是为处理类似情况而设计的。 披露:我是该产品的作者。

I never met such appender also, but I don't think it's a big deal to write your own. If you want to do it yourself - look at AppenderSkeleton, minimum you will have to do is to override append(..) method. When your appender will be called, you will get fully formed event which will contain everything needed, including NDC. Do whatever you like with it then.. I think MDC (mapped diagnostic context) in your case is viable alternative, it's more flexible IMHO.

If you're looking for commercial solution - look at logFaces, it was designed for handling similar situations. Disclosure: I am the author of this product.

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