java.io.NotSerializableException: org.apache.log4j.Logger mistake says that lawsuit of org.apache.lo4j.Logger is non Serializable. This mistake comes when nosotros travel log4j for logging inward Java in addition to practise Logger inward a Serializable shape e.g. whatever domain shape or POJO which nosotros desire to shop inward HttpSession or desire to serialize it. As nosotros know from 10 Java Serialization interview question that, if yous accept a non serializable shape every bit fellow member inward a Serializable class, it volition throw java.io.NotSerializableException Exception.
Look at the below code :
public shape Customer implements Serializable{
private Logger logger = Logger.getLogger(Customer.class)
......
}
If lawsuit of Customer volition live stored inward HttpSession or Serialized externally it volition throw "java.io.NotSerializableException: org.apache.log4j.Logger" because hither logger lawsuit is neither static or transient in addition to it doesn't implement Serializable or Externalzable interface.
How to solve java.io.NotSerializableException: org.apache.log4j.Logger
Solving java.io.NotSerializableException: org.apache.log4j.Logger is simple, yous accept to forestall logger lawsuit from default serializabtion process, either acquire far transient or static. Making it static lastly is preferred alternative due to many argue because if yous acquire far transient than later on deserialization logger lawsuit volition live null in addition to whatever logger.debug() telephone band volition final result inward NullPointerException inward Java because neither constructor non lawsuit initializer block is called during deserialization. By making it static in addition to lastly yous ensure that its thread-safe in addition to all lawsuit of Customer shape tin percentage same logger instance, By the means this mistake is too 1 of the argue Why Logger should live declared static in addition to final inward Java program. Just brand next code modify to cook java.io.NotSerializableException: org.apache.log4j.Logger inward Java.
public shape Customer implements Serializable{
private static lastly Logger logger = Logger.getLogger(Customer.class)
......
}
That's all on how to cook java.io.NotSerializableException: org.apache.log4j.Logger in Java. We accept seen what crusade java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger shape is non Serializable simply nosotros too learned that at that spot is no holler for serializing Logger lawsuit in addition to amend to brand Logger static in addition to final.
Further Learning
Complete Java Masterclass
2 ways to solve java.lang.OutOfMemoryError inward Java