日志记录包装器 - log4j
我需要做一个包装器接口,以便您可以在不同类型的日志记录机制之间切换而无需更改代码。所以我在包装类中有自己的 info(String message) 方法,例如:
public void info( Object message ) { if ( isInfoEnabled() ) { logger.info( 消息 ); } 它
按预期工作,只是在记录时它不显示我正在记录的位置的类和行,而是显示来自 Wrapper 类的类和行。 你能帮忙吗...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是包装 log4j 的方式。您向
log()
传递包装类的名称。This is how you wrap log4j. You pass
log()
the name of your wrapper class.因此,为了解决我的具体要求,应该这样做...
{
私人记录器记录器;
So, to solve my specific requirement, here is it what should be done...
{
private Logger logger;
已经有一个围绕各种日志记录实现的包装器接口:Apache Commons Logging。你尝试过用它来代替吗?
There's already a wrapper interface around various logging implementations: Apache Commons Logging. Have you tried using that instead?
我的建议是使用 Simple Logging Facade for Java (SLF4J) 而不是您自己的包装器。从其网站:
这是 Jakarta Commons Logging (JCL) 的更现代且首选的1替代方案。我建议阅读 这篇文章 进行详细分析,但简短的版本是 SLF4J 不会遇到使用 JCL 观察到的类加载器问题或内存泄漏问题。 Commons Logging 的作者本人承认了这一点。
如果您想知道,SLF4J 是由 Log4J 的作者 Ceki Gülcü 编写的(现在致力于 Logback,Log4J 的后继者)。因此,这个人对日志记录略知一二,并且“他的”包装器有可能比我的更好。
换句话说,恕我直言,当一个好的包装器已经存在时,在 2010 年创建一个包装器是没有意义的。
1 请不要使用 Spring 作为反例,Spring 是一个不好的反例(请参阅 SPR-5327)。
My suggestion would be to use Simple Logging Facade for Java (SLF4J) instead of your own wrapper. From its website:
This is a moderner and preferred1 alternative to Jakarta Commons Logging (JCL). I recommend to read this article for a detailed analysis but the short version is that SLF4J doesn't suffer from the class loader problems or memory leaks observed with JCL. The author of Commons Logging himself admits it.
In case you wonder, SLF4J has been written by Ceki Gülcü which is the author of Log4J (and now works on Logback, the successor of Log4J). So this guy know a thing or two about logging and there are some chances "his" wrapper is better than mine.
In other words, there is IMHO just no point at creating a wrapper in 2010 when a nice and good one already exists.
1 Please, don't use Spring as counter example, Spring is a bad counter example (see SPR-5327).