如何配置 log4j 进行包级别日志记录?
我想登录多个日志文件(flume 和 console)。如何将log4j设置为包级别?即将com.mypackage.myclass设置为flume并将其他包设置为控制台..
I want to log in to multiple log files(flume and console). How to set log4j as package level?ie com.mypackage.myclass into flume and other packages into console..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要将 log4j 配置为具有两个命名记录器,一个发送到控制台附加程序,另一个发送到 Flume。然后,您可以编写使用代理类来进行日志记录调用,根据调用者所在的包将 log4j 调用路由到不同的记录器。您可以通过访问当前线程的堆栈来完成此操作,如下所示:
上面的代码假设您已将两个记录器命名为“Flume”和“Console”。
当您在应用程序中进行日志记录调用时,请使用 Logger.getLogger() 而不是直接转到 log4j。
First of all you need to configure log4j to have two named loggers, one that sends to the Console appender, and one that sends to Flume. You can then write use a proxy class for making your logging calls that routes the log4j calls to the different loggers depending on the package the caller is in. You can do this by accessing the stack of the current thread, like so:
The code above is assuming you have named your two loggers 'Flume' and 'Console'.
When ever you make a logging call in your app, use Logger.getLogger() rather than going to log4j directly.
检查这篇博文
http://veerasundar.com/ blog/2009/07/log4j-tutorial-adding-log4j-logging-to-your-project/
它有一个完整的 PDF 可供下载,介绍如何将 log4j 添加到项目中。
您需要为不同的包定义类别。所有内容都在上面的 PDF 中进行了解释。
希望有帮助。
Check this blog post
http://veerasundar.com/blog/2009/07/log4j-tutorial-adding-log4j-logging-to-your-project/
It has a complete PDF for download on how to add log4j to project.
You need to define categories for different packages.Everything is explained in above PDF.
Hope it helps.