如何在抽象类/继承中正确初始化 slf4j(使用 log4j)记录器

发布于 2024-09-26 22:49:14 字数 1434 浏览 6 评论 0原文

在我的项目中,我有一个顶级抽象类FrameProducer。我在这个级别添加了一个 slf4j 记录器,以便每个继承类都已经拥有它。代码如下:

public abstract class FrameProducer extends Observable {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    protected BufferedImage frame;

    public BufferedImage getFrame() {
        return frame;
    }

    public void fireEvent() {
        logger.debug("Firing event. Implementing class: {}", this.getClass());
        setChanged();
        notifyObservers();
    }
}

还有两个继承类:CameraFrameGrabberGrayscaleFilter。然而,当从 CameraFrameGrabberGrayscaleFilter 调用方法 fireEvent() 时,消息将记录在 FrameProducer 中等级。为了清楚起见,这里是日志:

FrameProducer.fireEvent - Firing event. Implementing class: class com.ofj.frameaccess.CameraFrameGrabber
FrameProducer.fireEvent - Firing event. Implementing class: class com.ofj.frameaccess.GrayscaleFilter

是否可以在FrameProducer中初始化记录器,以便所有内容都记录在我的类层次结构中最专业的级别?

谢谢任何帮助。


编辑:我的 log4j.properties 如下所示:

log4j.rootCategory=TRACE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%C{1}.%M - %m%n

In my project I've got a top-level abstract class FrameProducer. I added a slf4j logger at this level, so that every inheriting class already has it. Here the code:

public abstract class FrameProducer extends Observable {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    protected BufferedImage frame;

    public BufferedImage getFrame() {
        return frame;
    }

    public void fireEvent() {
        logger.debug("Firing event. Implementing class: {}", this.getClass());
        setChanged();
        notifyObservers();
    }
}

There are also two inheriting classes: CameraFrameGrabber and GrayscaleFilter. Yet, when the method fireEvent() is being called from CameraFrameGrabber or GrayscaleFilter the message is being logged at FrameProducer level. Here the log, for clarity:

FrameProducer.fireEvent - Firing event. Implementing class: class com.ofj.frameaccess.CameraFrameGrabber
FrameProducer.fireEvent - Firing event. Implementing class: class com.ofj.frameaccess.GrayscaleFilter

Is it possible to initialize the logger in FrameProducer in a way that everything gets logged at the most-specialized level in my class hierarchy?

Thanks for any help.


Edit: My log4j.properties looks like this:

log4j.rootCategory=TRACE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%C{1}.%M - %m%n

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

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

发布评论

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

评论(2

何其悲哀 2024-10-03 22:49:15

感谢斯卡夫曼指出了寻找可能更正的正确方向。我最终将我的 log4j.properties 更改为(作为 PatternLayout 文档 说):

log4j.rootCategory=TRACE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-6r [%15.15t] %-5p %30.30c %x - %m%n

...一切都被正确记录。

Thanks for pointing out the right direction to look for possible corrections skaffman. I eventually changed my log4j.properties to (as one of the examples in the PatternLayout documentation says):

log4j.rootCategory=TRACE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-6r [%15.15t] %-5p %30.30c %x - %m%n

...and everything gets logged right.

迷迭香的记忆 2024-10-03 22:49:14

某些人认为记录抽象类是一种反模式,因为它引入了潜在的意外依赖。但是,如果您确定您正在处理的所有内容都是您自己的,那么我想那没问题。

如果您想让日志消息显示具体实现而不是抽象类名称(这是有道理的),那么将抽象类中的日志初始化语句更改为:

private final Logger logger = LoggerFactory.getLogger(getClass());

而不是类似:

private static final Logger logger = LoggerFactory.getLogger(MyAbstractClass.class);

  • 没有静态,因为它可以转换为其他的东西。
  • getClass() 而不是具体的类

Logging in abstract classes is considered an anti-pattern by some because it introduces a potentially unexpected dependency. However if you are sure that all the stuff you are dealing with is your own that that's no problem I guess.

If you want to get the log message to show the concrete implementation rather than the abstract class name (which makes sense) then change your logging initialization statement in the abstract class to:

private final Logger logger = LoggerFactory.getLogger(getClass());

instead of something like:

private static final Logger logger = LoggerFactory.getLogger(MyAbstractClass.class);

i.e.

  • no static because it could be cast to something else.
  • getClass() instead of the specific class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文