How To Get Java.Io.Notserializableexception: Org.Apache.Log4j.Logger Fault Inwards Java

java.io.NotSerializableException: org.apache.log4j.Logger fault says that illustration of org.apache.lo4j.Logger is non Serializable. This fault comes when nosotros purpose log4j for logging inwards Java as well as practice Logger inwards a Serializable cast e.g. whatever domain cast or POJO which nosotros desire to shop inwards HttpSession or desire to serialize it. As nosotros know from 10 Java Serialization interview question that, if you lot accept a non serializable cast equally fellow member inwards a Serializable class, it volition throw java.io.NotSerializableException Exception.

Look at the below code :

public cast Customer implements Serializable{

private Logger logger =  Logger.getLogger(Customer.class)

......

}

If illustration of Customer volition live stored inwards HttpSession or Serialized externally it volition throw "java.io.NotSerializableException: org.apache.log4j.Logger" because hither logger illustration is neither static or transient as well as it doesn't implement Serializable or Externalzable interface.


How to solve java.io.NotSerializableException: org.apache.log4j.Logger

 as well as practice Logger inwards a Serializable cast e How to gear upward java.io.NotSerializableException: org.apache.log4j.Logger Error inwards JavaSolving java.io.NotSerializableException: org.apache.log4j.Logger  is simple, you lot accept to forbid logger illustration from default serializabtion process, either acquire inwards transient or static. Making it static lastly is preferred selection due to many argue because if you lot acquire inwards transient than afterward deserialization logger illustration volition live zip as well as whatever logger.debug() telephone phone volition final result inwards NullPointerException inwards Java because neither constructor non illustration initializer block is called during deserialization. By making it static as well as lastly you lot ensure that its thread-safe as well as all illustration of Customer cast tin percentage same logger instance, By the means this fault is equally good 1 of the argue Why Logger should live declared static as well as final inwards Java program. Just brand next code modify to gear upward java.io.NotSerializableException: org.apache.log4j.Logger inwards Java.

public cast Customer implements Serializable{

private static lastly Logger logger =  Logger.getLogger(Customer.class)

......

}

That's all on how to gear upward java.io.NotSerializableException: org.apache.log4j.Logger in Java. We accept seen what displace java.io.NotSerializableException: org.apache.log4j.Logger, it's because Logger cast is non Serializable simply nosotros equally good learned that in that location is no indicate serializing Logger illustration as well as amend to brand Logger static as well as final.

Further Learning
Complete Java Masterclass
2 ways to solve java.lang.OutOfMemoryError inwards Java

Belum ada Komentar untuk "How To Get Java.Io.Notserializableexception: Org.Apache.Log4j.Logger Fault Inwards Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel