Hello, Developers!

Makeing Fortress live inside a servlet need a logger for
the startup. Lateron I use LogKitLoggerManager configured
from file and it's going to use ServletLotTargetFactory too,
but untill the configuration is read in I need a bootstrap
logger ( ContextManager and DefaultContainerManager in Fortress
log to it ).

As this logger I use a logger that logs directly to the servlet
log. It is just a slightly modified ConsoleLogger. I beleive
that such logger is just as usefull for servlet environment as
a ConsoleLogger is usefull for startup in command-line environment.

I'm perfectly okay with having it in my own utils, but in
case it's welcome in the overall avalon too, I'm sending in
the file :-)

Where could this go?
Fear to say that, framework? :-)))
(Where other bootstrap loggers live.)

avalon-excalibur/logger?

WBR, Anton

package ws.atagunov.avalon.logger;

import org.apache.avalon.framework.logger.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

/**
 * Logger to stratup avalon application inside a servlet.
 * Intended to be used as a logger for Fortress
 * ContextManager/ContainerManager.
 * 
 * Adapted from ServletLogger.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a>
 */

public class ServletLogger implements Logger
{
    /** Typecode for debugging messages. */
    public static final int LEVEL_DEBUG = 0;

    /** Typecode for informational messages. */
    public static final int LEVEL_INFO = 1;

    /** Typecode for warning messages. */
    public static final int LEVEL_WARN = 2;

    /** Typecode for error messages. */
    public static final int LEVEL_ERROR = 3;

    /** Typecode for fatal error messages. */
    public static final int LEVEL_FATAL = 4;

    /** Typecode for disabled log levels. */
    public static final int LEVEL_DISABLED = 5;

    private final ServletContext m_servletContext;
    private final int m_logLevel;
    private final String m_servletName;

    /**
     * Creates a new ServletLogger with the priority set to DEBUG.
     */
    public ServletLogger( final ServletConfig servletConfig )
    {
        this( servletConfig, LEVEL_DEBUG );
    }

    /**
     * Creates a new ServletLogger.
     * @param logLevel log level typecode
     */
    public ServletLogger( final ServletConfig servletConfig, final int logLevel )
    {
        if ( servletConfig == null )
        {
            throw new NullPointerException( "servletConfig" );
        }
        m_servletContext = servletConfig.getServletContext();
        if ( m_servletContext == null )
        {
            throw new NullPointerException( "servletConfig.getServletContext()" );
        }
        final String servletName = servletConfig.getServletName();
        m_servletName = servletName == null || "".equals( servletName ) ? "unknown" : 
servletName;
        m_logLevel = logLevel;
    }

    /**
     * Logs a debugging message.
     *
     * @param message a <code>String</code> value
     */
    public void debug( final String message )
    {
        debug( message, null );
    }

    /**
     * Logs a debugging message and an exception.
     *
     * @param message a <code>String</code> value
     * @param throwable a <code>Throwable</code> value
     */
    public void debug( final String message, final Throwable throwable )
    {
        if( m_logLevel <= LEVEL_DEBUG )
        {
            m_servletContext.log( m_servletName + ": [DEBUG] " + message, throwable );
        }
    }

    /**
     * Returns <code>true</code> if debug-level logging is enabled, false otherwise.
     *
     * @return <code>true</code> if debug-level logging
     */
    public boolean isDebugEnabled()
    {
        return m_logLevel <= LEVEL_DEBUG;
    }

    /**
     * Logs an informational message.
     *
     * @param message a <code>String</code> value
     */
    public void info( final String message )
    {
        info( message, null );
    }

    /**
     * Logs an informational message and an exception.
     *
     * @param message a <code>String</code> value
     * @param throwable a <code>Throwable</code> value
     */
    public void info( final String message, final Throwable throwable )
    {
        if( m_logLevel <= LEVEL_INFO )
        {
            m_servletContext.log( m_servletName + ": [INFO] " + message, throwable );
        }
    }

    /**
     * Returns <code>true</code> if info-level logging is enabled, false otherwise.
     *
     * @return <code>true</code> if info-level logging is enabled
     */
    public boolean isInfoEnabled()
    {
        return m_logLevel <= LEVEL_INFO;
    }

    /**
     * Logs a warning message.
     *
     * @param message a <code>String</code> value
     */
    public void warn( final String message )
    {
        warn( message, null );
    }

    /**
     * Logs a warning message and an exception.
     *
     * @param message a <code>String</code> value
     * @param throwable a <code>Throwable</code> value
     */
    public void warn( final String message, final Throwable throwable )
    {
        if( m_logLevel <= LEVEL_WARN )
        {
            m_servletContext.log( m_servletName + ": [WARNING] " + message, throwable 
);
        }
    }

    /**
     * Returns <code>true</code> if warn-level logging is enabled, false otherwise.
     *
     * @return <code>true</code> if warn-level logging is enabled
     */
    public boolean isWarnEnabled()
    {
        return m_logLevel <= LEVEL_WARN;
    }

    /**
     * Logs an error message.
     *
     * @param message a <code>String</code> value
     */
    public void error( final String message )
    {
        error( message, null );
    }

    /**
     * Logs an error message and an exception.
     *
     * @param message a <code>String</code> value
     * @param throwable a <code>Throwable</code> value
     */
    public void error( final String message, final Throwable throwable )
    {
        if( m_logLevel <= LEVEL_ERROR )
        {
            m_servletContext.log( m_servletName + ": [ERROR] " + message, throwable );
        }
    }

    /**
     * Returns <code>true</code> if error-level logging is enabled, false otherwise.
     *
     * @return <code>true</code> if error-level logging is enabled
     */
    public boolean isErrorEnabled()
    {
        return m_logLevel <= LEVEL_ERROR;
    }

    /**
     * Logs a fatal error message.
     *
     * @param message a <code>String</code> value
     */
    public void fatalError( final String message )
    {
        fatalError( message, null );
    }

    /**
     * Logs a fatal error message and an exception.
     *
     * @param message a <code>String</code> value
     * @param throwable a <code>Throwable</code> value
     */
    public void fatalError( final String message, final Throwable throwable )
    {
        if( m_logLevel <= LEVEL_FATAL )
        {
            m_servletContext.log( m_servletName + ": [FATAL ERROR] " + message, 
throwable );
        }
    }

    /**
     * Returns <code>true</code> if fatal-level logging is enabled, false otherwise.
     *
     * @return <code>true</code> if fatal-level logging is enabled
     */
    public boolean isFatalErrorEnabled()
    {
        return m_logLevel <= LEVEL_FATAL;
    }

    /**
     * Just returns this logger (<code>ServletLogger</code> is not hierarchical).
     *
     * @param name ignored
     * @return this logger
     */
    public Logger getChildLogger( final String name )
    {
        return this;
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to