commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject [logging] Avalon log not Serializable?
Date Tue, 21 Feb 2006 04:59:51 GMT
Hi,

I've been checking whether all the standard Log adapter classes are
Serializable. The Avalon logger declares itself to be Serializable, but
my reading of the code indicates this won't work. Can someone
confirm/deny this?

The javadoc has this:
 * <strong>Note:</strong> <code>AvalonLogger</code> implements
 * <code>Serializable</code> for reasons of consistency and backwards
compatibility. 
 * However, serializable is not recommended.

Here's the implementation:

    /** Avalon logger used to perform log */
    private transient Logger logger = null;

    public Logger getLogger() {
        return logger;
    }

    public boolean isDebugEnabled() {
        return getLogger().isDebugEnabled();
    }


When an object is deserialized, member "logger" will end up as null.
Note that this is because it gets the *default* value for that type;
instance member initialisers are not run. A call to isDebugEnabled will
then result in a NullPointerException.

There is no readObject or readResolve method to customise serialization
behaviour.


The Log4JLogger class appears to work correctly, because it implements
getLogger like this:
    public Logger getLogger() {
        if (logger == null) {
            logger = Logger.getLogger(name);
        }
        return (this.logger);
    }
which reinitialises the logger member after deserialization. Personally
I would like to see a readObject method do this instead as all other
methods could then access the logger member directly and avoid a method
call; however both will work fine. Hmm..perhaps this method call is the
cause of the slowdown in Log4JLogger since 1.2 revealed by Robert's
recent tests?

Perhaps we should implement AvalonLogger.getLogger like this?
    public Logger getLogger() {
        if (logger == null) {
            if (defaultLogger != null) {
              logger = defaultLogger.getChildLogger(name);
            } else {
              throw new NotSerializableException(
                "AvalonLogger is only deserializable when" +
                 "AvalonLogger.setDefaultLogger has been called");
            }
        }

        return logger;
    }



All the other loggers appear ok:
 * Jdk14Logger uses the same approach as Log4JLogger.
 * LogKitLogger does the same.
 * SimpleLog doesn't have an "underlying Log object" to handle;
   it just needs to serialize its own primitive config members
   which looks like it will work fine.
 * NoOpLog doesn't have any problem (nothing to serialize).


Cheers,

Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message