Have you seen an appender that would log to separate files based on NDC in Log4j? [closed]

巧了我就是萌 提交于 2019-12-04 13:48:41

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.

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.

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.

Dima

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!