You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Niall Pemberton <ni...@blueyonder.co.uk> on 2005/11/23 03:11:59 UTC

[logging] Advice on static/instance/transient Log variables

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

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



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


Re: [logging] Advice on static/instance/transient Log variables

Posted by Niall Pemberton <ni...@gmail.com>.
On 11/23/05, Craig McClanahan <cr...@apache.org> wrote:
> On 11/22/05, Niall Pemberton <ni...@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.

Thanks for responding. I'll go with this approach then.

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

+1

Niall

> Craig
>
>

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


Re: [logging] Advice on static/instance/transient Log variables

Posted by Craig McClanahan <cr...@apache.org>.
On 11/22/05, Niall Pemberton <ni...@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