You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Richard Mixon (qwest)" <rn...@qwest.net> on 2005/02/12 01:23:25 UTC

commons-logging logger instances - how to initialize in replicated session objects

I have been working with Filip Hanik to get failover/session replication
working for my application. Finally it is working quite well. Hooray for
Filip!

However it did uncover one issue with the way I was handling the
commons-logging logger instances in my business objects that I store in
the HTTP session at times.

Given the following class:
  public class MyBusinessObject1 implements Serializable {
    private static Log log =
LogFactory.getLog(ClassMeasurementFilter.class);
    <SNIP>
    public void setShowXAxisAsPercentages(boolean
showXAxisAsPercentages) {
        this.showXAxisAsPercentages = showXAxisAsPercentages;
        if (log == null) {
            log = LogFactory.getLog(ClassMeasurementFilter.class);
            log.info("setShowXAxisAsPercentages - log was null");
        }
        if (log.isDebugEnabled()) log.debug("setShowXAxisAsPercentages
set to "+this.showXAxisAsPercentages);
    }
    <SNIP>

I had to put the "if (log == null)" check in because when the objects
were replicated to another cluster instance and failover occured I would
get an NPE on the "log" variable.

Obviously having to put this "if (log == null)" check in is very awkward
and error prone.

Other approaches include creating a utility method in the base class for
my business objects. I'm not sure if this will give me the "logger per
class" that I've been used to so that I can readily identify where log
messages come from.

Are there any other approaches anyone would suggest?

Thank you - Richard


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


Re: commons-logging logger instances - how to initialize in replicated session objects

Posted by Roberto Cosenza <ro...@gmail.com>.
Richard, You should also be carefull with the "static" keyword if you
are using a clustered session. Its meaning is completely different
that when running in just one jvm so I would remove it to avoid
confusion.
Since you don't need to replicate the loggers, you could also define
them as "transient", so they will not be serialized during session
replication.
In my classes, I usually use a "protected transient Log logger"
declaration so you can also avoid to redeclare loggers for your
subclasses.


On Sat, 12 Feb 2005 06:43:03 -0700, Richard Mixon (qwest)
<rn...@qwest.net> wrote:
> Thanks Trond, I had forgotten about readObject.That may be a better
> option than creating yet another utility method.
> 
> Trond G. Ziarkowski wrote:
> > Hi,
> >
> > I'm maybe stepping out of my territory here, but I think that static
> > members are not serialized/deserialized. To re-initialize your static
> > logger maybe you should try to implement the
> > readObject(java.io.ObjectInputStream in) method from the
> > java.io.Serializable interface something like this:
> >
> > readObject(...) {
> >     super.readObject(...);
> >     log = LogFactory.getLog(...);
> > }
> >
> > Hope this is of some help
> >
> > Trond
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 


-- 
Roberto Cosenza
http://robcos.com

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


RE: SOLVED - commons-logging logger instances - how to initialize in replicated session objects

Posted by "Richard Mixon (qwest)" <rn...@qwest.net>.
In case anyone else ends up with similar problems trying to get session
replication to work for objects that have a commons logging (or other
similar) non-serializable instance variable), here's how I solved it.

I created the following abstract class with the two methods "readObject"
and "readObjectNoData" (not sure this one is really needed). All of my
classes that might bet saved in a session and be replicated drive
directly/indirectly from this class. I was hoping for something a bit
more elegant, but it works for now.

Thanks Trond for the idea.

Any comments or criticisms are welcome.

Thanks - Richard

package com.acme.common.util;
import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**Loggable Object Class.
 * <p>Abstract class to derive business and utility
 * objects from that require logging support and that
 * may be serialized (e.g. for Session replication, etc.).
 */
public abstract class LoggableObject implements Serializable {
    private static final long serialVersionUID = 1L;
    public transient Log log = LogFactory.getLog(getClass().getName());
    private void readObject(java.io.ObjectInputStream stream) throws
java.io.IOException, ClassNotFoundException {
        stream.defaultReadObject(); // Let default behaviour occur (i.e.
handle non-static, non-transient fields)
        if (log == null) { // Initialize log instance if it is null
            log = LogFactory.getLog(getClass().getName());
        }
    }
   private void readObjectNoData() throws java.io.ObjectStreamException
{
        if (log == null) { // Initialize log instance if it is null
            log = LogFactory.getLog(getClass().getName());
        }
    }
}


-----Original Message-----
From: Richard Mixon (qwest) [mailto:rnmixon@qwest.net]
Sent: Saturday, February 12, 2005 6:43 AM
To: Tomcat Users List
Subject: RE: commons-logging logger instances - how to initialize in
replicated session objects


Thanks Trond, I had forgotten about readObject.That may be a better
option than creating yet another utility method.

Trond G. Ziarkowski wrote:
> Hi,
>
> I'm maybe stepping out of my territory here, but I think that static
> members are not serialized/deserialized. To re-initialize your static
> logger maybe you should try to implement the
> readObject(java.io.ObjectInputStream in) method from the
> java.io.Serializable interface something like this:
>
> readObject(...) {
>     super.readObject(...);
>     log = LogFactory.getLog(...);
> }
>
> Hope this is of some help
>
> Trond
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


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




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


RE: commons-logging logger instances - how to initialize in replicated session objects

Posted by "Richard Mixon (qwest)" <rn...@qwest.net>.
Thanks Trond, I had forgotten about readObject.That may be a better
option than creating yet another utility method.

Trond G. Ziarkowski wrote:
> Hi,
>
> I'm maybe stepping out of my territory here, but I think that static
> members are not serialized/deserialized. To re-initialize your static
> logger maybe you should try to implement the
> readObject(java.io.ObjectInputStream in) method from the
> java.io.Serializable interface something like this:
>
> readObject(...) {
>     super.readObject(...);
>     log = LogFactory.getLog(...);
> }
>
> Hope this is of some help
>
> Trond
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


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


Re: commons-logging logger instances - how to initialize in replicated session objects

Posted by "Trond G. Ziarkowski" <tr...@gep-as.com>.
Hi,

I'm maybe stepping out of my territory here, but I think that static 
members are not serialized/deserialized. To re-initialize your static 
logger maybe you should try to implement the 
readObject(java.io.ObjectInputStream in) method from the 
java.io.Serializable interface something like this:

readObject(...) {
    super.readObject(...);
    log = LogFactory.getLog(...);
}

Hope this is of some help

Trond


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