Should a Log4J logger be declared as transient?

Static fields are not serialized by default; you can explicitly declare fields to serialize with a private, static, final array of ObjectStreamField named serialPersistentFields See Oracle documentation.

Static fields are not serialized by default; you can explicitly declare fields to serialize with a private, static, final array of ObjectStreamField named serialPersistentFields. See Oracle documentation Added 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.

The logger must be static; this would make it non-serializable. There's no reason to make logger non-static, unless you have a strong reason to do it so.

2 It can sometimes be useful to have a non-static logger if you want to use different logger names for each instance (e.g. Appending an instance-unique string onto the end of the logger name), or if you have a logger in a superclass object and you want it to use the name of the subclass instance. – MB. Sep 17 '08 at 11:53.

Either declare your logger field as static or as transient. Both ways ensure the writeObject() method will not attempt to write the field to the output stream during serialization. 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.

If you really 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: private void readObject(java.io. ObjectInputStream in) throws IOException, ClassNotFoundException; The javadocs for Serializable has information on this method.

Your implementation of it will look something like: private void readObject(java.io. ObjectInputStream in) throws IOException, ClassNotFoundException { log = Logger. GetLogger(...); in.

DefaultReadObject(); } If you do not do this then log will be null after deserializing your object.

Try making the Logger static instead. Than you don't have to care about serialization because it is handled by the class loader.

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. 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.

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.

Loggers are not serializable so you must use transient when storing them in instance fields. If you want to restore the logger after deserialization you can store the Level (String) indide your object which does get serialized.

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). (Like others have mentioned use static or transient).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions