I am using Java 1.4 with Log4J.
Some of my code involves serializing and deserializing value objects (POJOs).
Each of my POJOs declares a logger with
private final Logger log = Logger.getLogger(getClass());
The serializer complains of org.apache.log4j.Logger not being Serializable.
Should I use
private final transient Logger log = Logger.getLogger(getClass());
instead?
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
ObjectStreamFieldnamedserialPersistentFields. See Oracle documentationAdded content: As you use getLogger(getClass()), 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.