commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig McClanahan <craig...@apache.org>
Subject Re: [logging] Advice on static/instance/transient Log variables
Date Wed, 23 Nov 2005 02:40:47 GMT
On 11/22/05, Niall Pemberton <niall.pemberton@blueyonder.co.uk> wrote:
>
> I recently noticed a change in BeanUtils that indicated its not a good
> idea
> to use static Log variables in a shared classloader environment:
>
>    http://svn.apache.org/viewcvs.cgi?rev=189535&view=rev<http://svn.apache.org/viewcvs.cgi?rev=189535&view=rev>
>
> Commons Resources uses static Log variables throughout and I'm thinking
> they
> should be changed to instance variables since it will be used in a shared
> classloader environment (e.g. webapps). Is this the current advice/good
> idea?
>
> If that is a recommended practice, my next thought is should the instance
> variables be made transient? I recently had a problem with a Log
> implementation not being Serializable, plus it would seem a good idea to
> not
> serialize the Log for all instances that use it. If this is a good idea
> I'm
> thinking that I should access the Log through a private method, so that
> the
> it can be re-initialized after de-serialization (rather than having to
> implement custom methods to serialize/deserialize). Something along the
> lines of this...
>
>     private transient Log log = LogFactory.getLog (Foo.class);
>
>     public void someMethod() {
>
>         if (getLog().isDebugEnabled()) {
>             getLog().debug("Foo message");
>         }
>
>     }
>
>     private Log getLog() {
>         if (log == null) {
>             log = LogFactory.getLog(Foo.class)
>         }
>         return log;
>     }
>
> Feedback/comments/advice would be appreciated.
>
> Niall


Seems like a reasonable approach.  The scenario that messes you up (Log
instances loaded from the webapp class loader while the Resources instance
is loaded from the shared class loader) seems like a mis-configuration to
me, but there's no harm in avoiding a potential problem.

FWIW, we should review Digester and all the other Struts dependencies for
the same issue.


Craig

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message