在Serializable Java类中使用Logger的正确方法是什么? [英] What is the proper way to use a Logger in a Serializable Java class?

查看:651
本文介绍了在Serializable Java类中使用Logger的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作的系统中有以下( doctored )类,并且 Findbugs 正在生成 SE_BAD_FIELD 警告,我正在尝试理解为什么会这样做在我按照我认为的方式修复它之前说。我感到困惑的原因是因为描述似乎表明我在类中没有使用其他非可序列化的实例字段,但bar.model.Foo也不是可序列化的并且以完全相同的方式使用(就我而言)可以告诉)但Findbugs没有生成任何警告。

I have the following (doctored) class in a system I'm working on and Findbugs is generating a SE_BAD_FIELD warning and I'm trying to understand why it would say that before I fix it in the way that I thought I would. The reason I'm confused is because the description would seem to indicate that I had used no other non-serializable instance fields in the class but bar.model.Foo is also not serializable and used in the exact same way (as far as I can tell) but Findbugs generates no warning for it.

import bar.model.Foo;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Demo implements Serializable {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final File file;
    private final List<Foo> originalFoos;
    private Integer count;
    private int primitive = 0;

    public Demo() {
        for (Foo foo : originalFoos) {
            this.logger.debug(...);
        }
    }

    ...

}

我对解决方案的初步看法是在我使用它时从工厂获得记录器参考:

My initial blush at a solution is to get a logger reference from the factory right as I use it:

public DispositionFile() {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    for (Foo foo : originalFoos) {
        this.logger.debug(...);
    }
}

但这似乎并不是特别有效。

That doesn't seem particularly efficient, though.

想法?

推荐答案

首先,不要过早优化。它可能是 LoggerFactory.getLogger()足够快,并且不会给执行时间带来很大的开销。如果有疑问,请对其进行分析。

Firstly, don't optimize prematurely. It may be that LoggerFactory.getLogger() is fast enough, and contributes no significant overhead to execution time. If in doubt, profile it.

其次,findbugs没有抱怨使用 Foo 是因为该类没有 Foo 类型的字段,它有一个 List 类型的字段。泛型在编译时被擦除,就字段定义而言,类中没有实际引用 Foo 。在运行时,如果您尝试序列化 Demo的实例 Foo 是不可序列化的这一事实会导致异常>类,但是findbugs不知道这个。

Secondly, the reason that findbugs isn't complaining about the use of Foo is because the class doesn't have a field of type Foo, it has a field of type List. The generics are erased at compile time, there is no actual reference to Foo in the class, as far as the field definition is concerned. At runtime, the fact that Foo is non-serializable would cause an exception if you tried to serialize an instance of the Demo class, but findbugs can't know this.

我的第一反应是让 Logger 静态字段,而不是实例字段。在这种情况下应该可以正常工作。

My first reaction would be to make the Logger a static field, rather than an instance field. Should work fine in this situation.

public class Demo implements Serializable {
   private static final Logger logger = LoggerFactory.getLogger(Demo.class);

   // .. other stuff
}

这篇关于在Serializable Java类中使用Logger的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆