FileDocCategorySizeDatePackage
LoggerBase.javaAPI DocGlassfish v2 API17399Fri May 04 22:32:10 BST 2007org.apache.catalina.logger

LoggerBase

public class LoggerBase extends Object implements org.apache.catalina.Lifecycle, org.apache.catalina.Logger, MBeanRegistration
Convenience base class for Logger implementations. The only method that must be implemented is log(String msg), plus any property setting and lifecycle methods required for configuration.
author
Craig R. McClanahan
version
$Revision: 1.5 $ $Date: 2007/05/05 05:32:09 $

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
protected org.apache.catalina.Container
container
The Container with which this Logger has been associated.
protected int
debug
The debugging detail level for this component.
protected static final String
info
The descriptive information about this implementation.
protected org.apache.catalina.util.LifecycleSupport
lifecycle
The lifecycle event support for this component.
protected PropertyChangeSupport
support
The property change support for this component.
protected int
verbosity
The verbosity level for above which log messages may be filtered.
protected String
domain
protected String
host
protected String
path
protected ObjectName
oname
protected ObjectName
controller
protected MBeanServer
mserver
Constructors Summary
Methods Summary
public voidaddLifecycleListener(org.apache.catalina.LifecycleListener listener)
Add a lifecycle event listener to this component.

param
listener The listener to add


        lifecycle.addLifecycleListener(listener);

    
public voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a property change listener to this component.

param
listener The listener to add


        support.addPropertyChangeListener(listener);

    
public javax.management.ObjectNamecreateObjectName()

        log("createObjectName with "+container);
        // register
        try {
            StandardEngine engine=null;            
            String suffix="";
            if( container instanceof StandardEngine ) {
                engine=(StandardEngine)container;                
            } else if( container instanceof StandardHost ) {
                engine=(StandardEngine)container.getParent();
                suffix=",host=" + container.getName();
            } else if( container instanceof StandardContext ) {
                String path = ((StandardContext)container).getPath();
                // add "/" to avoid MalformedObjectName Exception
                if (path.equals("")) {
                    path = "/";
                }
                engine=(StandardEngine)container.getParent().getParent();
                suffix= ",path=" + path + ",host=" + 
                        container.getParent().getName();
            } else {
                log.error("Unknown container " + container );
            }
            if( engine != null ) {
                oname=new ObjectName(engine.getDomain()+ ":type=Logger" + suffix);
            } else {
                log.error("Null engine !! " + container);
            }
        } catch (Throwable e) {
            e.printStackTrace();  //To change body of catch statement use Options | File Templates.
        }
        return oname;
    
public voiddestroy()

        
    
public org.apache.catalina.LifecycleListener[]findLifecycleListeners()
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.


        return lifecycle.findLifecycleListeners();

    
public org.apache.catalina.ContainergetContainer()
Return the Container with which this Logger has been associated.



    // ------------------------------------------------------------- Properties


                   
       

        return (container);

    
public javax.management.ObjectNamegetController()

        return controller;
    
public intgetDebug()
Return the debugging detail level for this component.


        return (this.debug);

    
public java.lang.StringgetDomain()

        return domain;
    
public java.lang.StringgetInfo()
Return descriptive information about this Logger implementation and the corresponding version number, in the format <description>/<version>.


        return (info);

    
public javax.management.ObjectNamegetObjectName()

        return oname;
    
public intgetVerbosity()
Return the verbosity level of this logger. Messages logged with a higher verbosity than this level will be silently ignored.


        return (this.verbosity);

    
public voidinit()

        
    
public voidlog(java.lang.String msg)
Writes the specified message to a servlet log file, usually an event log. The name and type of the servlet log is specific to the servlet container. This message will be logged unconditionally.

param
message A String specifying the message to be written to the log file

        log.debug(msg);
    
public voidlog(java.lang.Exception exception, java.lang.String msg)
Writes the specified exception, and message, to a servlet log file. The implementation of this method should call log(msg, exception) instead. This method is deprecated in the ServletContext interface, but not deprecated here to avoid many useless compiler warnings. This message will be logged unconditionally.

param
exception An Exception to be reported
param
msg The associated message string


        log(msg, exception);

    
public voidlog(java.lang.String msg, java.lang.Throwable throwable)
Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file. The name and type of the servlet log file is specific to the servlet container, usually an event log. This message will be logged unconditionally.

param
msg A String that describes the error or exception
param
throwable The Throwable error or exception


        CharArrayWriter buf = new CharArrayWriter();
        PrintWriter writer = new PrintWriter(buf);
        writer.println(msg);
        throwable.printStackTrace(writer);
        Throwable rootCause = null;
        if (throwable instanceof LifecycleException)
            rootCause = ((LifecycleException) throwable).getThrowable();
        else if (throwable instanceof ServletException)
            rootCause = ((ServletException) throwable).getRootCause();
        if (rootCause != null) {
            writer.println("----- Root Cause -----");
            rootCause.printStackTrace(writer);
        }
        log(buf.toString());

    
public voidlog(java.lang.String message, int verbosity)
Writes the specified message to the servlet log file, usually an event log, if the logger is set to a verbosity level equal to or higher than the specified value for this message.

param
message A String specifying the message to be written to the log file
param
verbosity Verbosity level of this message


        if (this.verbosity >= verbosity)
            log(message);

    
public voidlog(java.lang.String message, java.lang.Throwable throwable, int verbosity)
Writes the specified message and exception to the servlet log file, usually an event log, if the logger is set to a verbosity level equal to or higher than the specified value for this message.

param
message A String that describes the error or exception
param
throwable The Throwable error or exception
param
verbosity Verbosity level of this message


        if (this.verbosity >= verbosity)
            log(message, throwable);

    
public voidpostDeregister()

    
public voidpostRegister(java.lang.Boolean registrationDone)

    
public voidpreDeregister()

    
public javax.management.ObjectNamepreRegister(javax.management.MBeanServer server, javax.management.ObjectName name)

        oname=name;
        mserver=server;
        // FIXME null pointer exception 
        if (name == null) {
            return null;
        }
        domain=name.getDomain();
        host=name.getKeyProperty("host");
        path=name.getKeyProperty("path");
        log("preRegister with "+name);
        if( container== null ) {
            // Register with the parent
            try {
                ObjectName cname=null;
                if( host == null ) {
                    // global
                    cname=new ObjectName(domain +":type=Engine");
                } else if( path==null ) {
                    cname=new ObjectName(domain +
                            ":type=Host,host=" + host);
                } else {
                    cname=new ObjectName(domain +":j2eeType=WebModule,name=//" +
                            host + "/" + path);
                }
                log.debug("Register with " + cname);
                mserver.invoke(cname, "setLogger", new Object[] {this},
                        new String[] {"org.apache.catalina.Logger"});
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
        } 
                
        return name;
    
public voidremoveLifecycleListener(org.apache.catalina.LifecycleListener listener)
Remove a lifecycle event listener from this component.

param
listener The listener to add


        lifecycle.removeLifecycleListener(listener);

    
public voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a property change listener from this component.

param
listener The listener to remove


        support.removePropertyChangeListener(listener);

    
public voidsetContainer(org.apache.catalina.Container container)
Set the Container with which this Logger has been associated.

param
container The associated Container


        Container oldContainer = this.container;
        this.container = container;
        support.firePropertyChange("container", oldContainer, this.container);

    
public voidsetController(javax.management.ObjectName controller)

        this.controller = controller;
    
public voidsetDebug(int debug)
Set the debugging detail level for this component.

param
debug The new debugging detail level


        this.debug = debug;

    
public voidsetVerbosity(int verbosity)
Set the verbosity level of this logger. Messages logged with a higher verbosity than this level will be silently ignored.

param
verbosity The new verbosity level


        this.verbosity = verbosity;

    
public voidsetVerbosityLevel(java.lang.String verbosity)
Set the verbosity level of this logger. Messages logged with a higher verbosity than this level will be silently ignored.

param
verbosityLevel The new verbosity level, as a string


        if ("FATAL".equalsIgnoreCase(verbosity))
            this.verbosity = FATAL;
        else if ("ERROR".equalsIgnoreCase(verbosity))
            this.verbosity = ERROR;
        else if ("WARNING".equalsIgnoreCase(verbosity))
            this.verbosity = WARNING;
        else if ("INFORMATION".equalsIgnoreCase(verbosity))
            this.verbosity = INFORMATION;
        else if ("DEBUG".equalsIgnoreCase(verbosity))
            this.verbosity = DEBUG;

    
public voidstart()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

exception
LifecycleException if this component detects a fatal error that prevents this component from being used

                                                                
        // register this logger
        if ( getObjectName()==null ) {   
            ObjectName oname = createObjectName();   
            try {   
                Registry.getRegistry().registerComponent(this, oname, null);
                if (log.isDebugEnabled()) {
                    log.debug("Registering logger " + oname);
                }
            } catch( Exception ex ) {   
                log.error( "Can't register logger " + oname, ex);   
            }      
        }     

    
public voidstop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

exception
LifecycleException if this component detects a fatal error that needs to be reported


        // unregister this logger
        if ( getObjectName()!=null ) {   
            ObjectName oname = createObjectName();   
            try {   
                Registry.getRegistry().unregisterComponent(oname); 
                if (log.isDebugEnabled()) {
                    log.debug("Unregistering logger " + oname);
                }
            } catch( Exception ex ) {   
                log.error( "Can't unregister logger " + oname, ex);   
            }      
        }