Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper way to use a Logger in a Serializable Java class?
    text
    copied!<p>I have the following (<em>doctored</em>) class in a system I'm working on and <a href="http://findbugs.sourceforge.net/" rel="noreferrer">Findbugs</a> is generating a <a href="http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD" rel="noreferrer">SE_BAD_FIELD</a> 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.</p> <pre><code>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&lt;Foo&gt; originalFoos; private Integer count; private int primitive = 0; public Demo() { for (Foo foo : originalFoos) { this.logger.debug(...); } } ... } </code></pre> <p>My initial blush at a solution is to get a logger reference from the factory right as I use it:</p> <pre><code>public DispositionFile() { Logger logger = LoggerFactory.getLogger(this.getClass()); for (Foo foo : originalFoos) { this.logger.debug(...); } } </code></pre> <p>That doesn't seem particularly efficient, though.</p> <p>Thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload