将 System.out.println 重定向到 Log4J,同时保留类名信息
我有一些库正在调用 System.out.println,我想通过 log4j 或 commons 日志记录重定向它们。但我特别想保留完全限定的类名,这样我就知道哪个组件生成了日志。
有没有一种好的、有序的方法来完成这个任务?
更新:完成此操作后,我在此处发布了代码:
http://www.bukisa.com/articles/487009_java-how-to-redirect-stderr-and-stdout-to-commons-logging-with-the-calling-class
I have some libraries that are calling System.out.println on me, I'd like to redirect them through log4j or commons logging. But in particular I'd like to keep the fully-qualified-classname so I know what component generated the logs.
Is there a nice, orderly way to accomplish this?
UPDATE: After accomplishing this I posted the code here:
http://www.bukisa.com/articles/487009_java-how-to-redirect-stderr-and-stdout-to-commons-logging-with-the-calling-class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的唯一方法是编写自己的 PrintStream 实现,该实现在调用 println 方法时创建堆栈跟踪,以便计算出类名。这将是非常可怕的,但它应该可以工作...概念验证示例代码:(
在您的代码中,您可以让它记录到 log4j 而不是当然...或者也可能。)
The only way I can think of would be to write your own
PrintStream
implementation which created a stack trace when theprintln
method was called, in order to work out the class name. It would be pretty horrible, but it should work... proof of concept sample code:(In your code you would make it log to log4j instead of course... or possibly as well.)
如果您可以修改源代码,请查看 Eclipse 插件 Log4E。它提供了一个将 System.out.println 转换为记录器语句(以及许多其他处理日志记录的很酷的东西)的函数。
If you can modify the source code, then have a look at the Eclipse Plugin Log4E. It provides a function to convert System.out.println into logger statements (and many other cool stuff dealing with logging).