<p>I am using Java 1.4 with Log4J. </p>

<p>Some of my code involves serializing and deserializing value objects (POJOs). </p>

<p>Each of my POJOs declares a logger with</p>

<pre><code>private final Logger log = Logger.getLogger(getClass());
</code></pre>

<p>The serializer complains of org.apache.log4j.Logger not being Serializable.</p>

<p>Should I use</p>

<pre><code>private final transient Logger log = Logger.getLogger(getClass());
</code></pre>

<p>instead?</p>

[{'answer_id': 82130, 'author': 'mindas', 'author_id': 7345, 'author_profile': 'https://Stackoverflow.com/users/7345', 'pm_score': 4, 'selected': False, 'text': "<p>The logger must be static; this would make it non-serializable.</p>\n\n<p>There's no reason to make logger non-static, unless you have a strong reason to do it so.</p>\n"}, {'answer_id': 82132, 'author': 'Aleksi Yrttiaho', 'author_id': 11427, 'author_profile': 'https://Stackoverflow.com/users/11427', 'pm_score': 6, 'selected': True, 'text': '<p>How about using a static logger? Or do you need a different logger reference for each instance of the class? Static fields are not serialized by default; you can explicitly declare fields to serialize with a private, static, final array of <code>ObjectStreamField</code> named <code>serialPersistentFields</code>. <a href="http://java.sun.com/developer/technicalArticles/ALT/serialization/#2" rel="noreferrer">See Oracle documentation</a></p>\n\n<p>Added content: \nAs you use <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html#getLogger(java.lang.String)" rel="noreferrer">getLogger(getClass())</a>, you will use the same logger in each instance. If you want to use separate logger for each instance you have to differentiate on the name of the logger in the getLogger() -method. e.g. getLogger(getClass().getName() + hashCode()). You should then use the transient attribute to make sure that the logger is not serialized. </p>\n'}, {'answer_id': 82138, 'author': 'Philip Helger', 'author_id': 15254, 'author_profile': 'https://Stackoverflow.com/users/15254', 'pm_score': 2, 'selected': False, 'text': "<p>Try making the Logger static instead. Than you don't have to care about serialization because it is handled by the class loader.</p>\n"}, {'answer_id': 82275, 'author': 'MB.', 'author_id': 11961, 'author_profile': 'https://Stackoverflow.com/users/11961', 'pm_score': 0, 'selected': False, 'text': "<p>If you want the Logger to be per-instance then yes, you would want to make it transient if you're going to serialize your objects.  Log4J Loggers aren't serializable, not in the version of Log4J that I'm using anyway, so if you don't make your Logger fields transient you'll get exceptions on serialization.</p>\n"}, {'answer_id': 82304, 'author': 'Glever', 'author_id': 15504, 'author_profile': 'https://Stackoverflow.com/users/15504', 'pm_score': 0, 'selected': False, 'text': '<p>Loggers are not serializable so you must use transient when storing them in instance fields.\nIf you want to restore the logger after deserialization you can store the Level (String) indide your object which does get serialized.</p>\n'}, {'answer_id': 82551, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>These kinds of cases, particularly in EJB, are generally best handled via thread local state. Usually the use case is something like you have a particular transaction which is encountering a problem and you need to elevate logging to debug for that operation so you can generate detailed logging on the problem operation. Carry some thread local state across the transaction and use that to select the correct logger. Frankly I don't know where it would be beneficial to set the level on an INSTANCE in this environment because the mapping of instances into the transaction should be a container level function, you won't actually have control of which instance is used in a given transaction anyway.</p>\n\n<p>Even in cases where you're dealing with a DTO it is not generally a good idea to design your system in such a way that a given specific instance is required because the design can easily evolve in ways that make that a bad choice. You could come along a month from now and decide that efficiency considerations (caching or some other life cycle changing optimization) will break your assumption about the mapping of instances into units of work. </p>\n"}, {'answer_id': 82552, 'author': 'user9189', 'author_id': 9189, 'author_profile': 'https://Stackoverflow.com/users/9189', 'pm_score': 3, 'selected': False, 'text': '<p>Either declare your logger field as static or as transient. </p>\n\n<p>Both ways ensure the writeObject() method will not attempt to write the field to the output stream during serialization.</p>\n\n<p>Usually logger fields are declared static, but if you need it to be an instance field just declare it transient, as its usually done for any non-serializable field. Upon deserialization the logger field will be null, though, so you have to implement a readObject() method to initialize it properly.</p>\n'}, {'answer_id': 82565, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 3, 'selected': False, 'text': '<p>If you <em>really</em> want to go the transient approach you will need to reset the log when your object is deserialized.  The way to do that is to implement the method:</p>\n\n<pre><code> private void readObject(java.io.ObjectInputStream in) \n   throws IOException, ClassNotFoundException;\n</code></pre>\n\n<p>The javadocs for <a href="http://java.sun.com/javase/6/docs/api/java/io/Serializable.html" rel="noreferrer">Serializable</a> has information on this method.  </p>\n\n<p>Your implementation of it will look something like:</p>\n\n<pre><code> private void readObject(java.io.ObjectInputStream in) \n     throws IOException, ClassNotFoundException {\n   log = Logger.getLogger(...);\n   in.defaultReadObject();\n }\n</code></pre>\n\n<p>If you do not do this then log will be null after deserializing your object.</p>\n'}, {'answer_id': 84187, 'author': 'James A. N. Stauffer', 'author_id': 6770, 'author_profile': 'https://Stackoverflow.com/users/6770', 'pm_score': 0, 'selected': False, 'text': '<p>There are good reasons to use an instance logger.  One very good use case is so you can declare the logger in a super-class and use it in all sub-classes (the only downside is that logs from the super-class are attributed to the sub-class but it is usually easy to see that).</p>\n\n<p>(Like others have mentioned use static or transient).</p>\n'}]
['https://Stackoverflow.com/questions/82109', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15452/']