[ 
https://issues.apache.org/jira/browse/LOGGING-123?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Alexander Pogrebnyak updated LOGGING-123:
-----------------------------------------

    Attachment: LogLevel.java

More readable version of LogLevel.java

> Add a LogLevel enum with the usual suspects 
> (FATAL,ERROR,WARN,INFO,DEBUG,TRACE)
> -------------------------------------------------------------------------------
>
>                 Key: LOGGING-123
>                 URL: https://issues.apache.org/jira/browse/LOGGING-123
>             Project: Commons Logging
>          Issue Type: New Feature
>         Environment: N/A
>            Reporter: Alexander Pogrebnyak
>            Priority: Trivial
>         Attachments: LogLevel.java
>
>
> It is sometimes convenient to setup logging level dynamically.
> For example if you implement Command object pattern. Some of the command 
> require very detailed in your face logging (say at INFO, or even WARN 
> levels), for others DEBUG or even TRACE would do fine.
> With the current methods on Log interface you either have to do it through 
> different boolean settings, which quickly lead to a mess.
> More elegant solution is to define a log level object and just examine it 
> when doing logging.
> here is the use case
> final static Log LOGGER = ...;
> public class Command
> {
>   final LogLevel _log_level;
>   public Command ( final boolean is_logging_enabled )
>   {
>     LogLevel log_level = LogLevel.TRACE;
>     if ( is_logging_enabled )
>     {
>       log_level = LogLevel.INFO;
>     }
>     if ( ! log_level.isEnabledIn( LOGGER ) )
>     {
>       log_level = null;
>     } 
>     _log_level = log_level;
>   }
>   public void doCommand ( )
>   {
>     if ( _log_level != null )
>     {
>       _log_level.log( LOGGER, "Running command" );
>     }
>   }
> }
> The proposed solution is all done through the inversion of control, so it 
> does not require to change a single line in org.apache.commons.logging.Log 
> interface, although it might be useful to add these 3 methods:
> boolean isEnabledFor ( final LogLevel level );
> void log ( final LogLevel level, final String message );
> void log ( final LogLevel level, final String message, final Throwable t );
> Here is a full listing of proposed LogLevel.java
> ----------START OF LogLevel.java
> package org.apache.commons.logging;
> /**
>  * <p>Title: $RCSFile$</p>
>  * <p>Description: Declaration of class 
> org.apache.commons.logging.LogLevel</p>
>  * <p>Last updated: $Id$</p>
>  * <p>Created: May 7, 2008</p>
>  * @author Alexander S. Pogrebnyak
>  * @version $Revision$
>  */
> ///@name Imports
> //@{
> //@}
> /**
>  * Defines log levels
>  *
>  * @author Alexander S. Pogrebnyak
>  * @version $Revision$
>  */
> public final class LogLevel
> {
>     ///@name Constants
>     //@{
>         /**
>          * The <code>FATAL</code> level designates very severe error
>          * events that will presumably lead the application to abort.
>          */
>         public final static LogLevel FATAL =
>             new LogLevel(
>                 "FATAL",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isFatalEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.fatal( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.fatal( message, t );
>                     }
>                 }
>             );
>         /**
>          * The <code>ERROR</code> level designates error events that
>          * might still allow the application to continue running.
>          */
>         public final static LogLevel ERROR =
>             new LogLevel(
>                 "ERROR",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isErrorEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.error( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.error( message, t );
>                     }
>                 }
>             );
>         /**
>          * The <code>WARN</code> level designates potentially harmful
>          * situations.
>          */
>         public final static LogLevel WARN =
>             new LogLevel(
>                 "WARN",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isWarnEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.warn( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.warn( message, t );
>                     }
>                 }
>             );
>         /**
>          * The <code>INFO</code> level designates informational messages
>          * that highlight the progress of the application at coarse-grained
>          * level.
>          */
>         public final static LogLevel INFO =
>             new LogLevel(
>                 "INFO",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isInfoEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.info( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.info( message, t );
>                     }
>                 }
>             );
>         /**
>          * The <code>DEBUG</code> level designates fine-grained
>          * informational events that are most useful to debug an
>          * application.
>          */
>         public final static LogLevel DEBUG =
>             new LogLevel(
>                 "DEBUG",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isDebugEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.debug( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.debug( message, t );
>                     }
>                 }
>             );
>         /**
>          * The <code>TRACE</code> level designates finer-grained
>          * informational events than the <code>DEBUG</code> level.
>          */
>         public final static LogLevel TRACE =
>             new LogLevel(
>                 "TRACE",
>                 new ILevelHandler ( )
>                 {
>                     public boolean isEnabledIn ( final Log logger )
>                     {
>                         return logger.isTraceEnabled( );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message
>                         )
>                     {
>                         logger.trace( message );
>                     }
>                     public void log (
>                             final Log logger,
>                             final Object message,
>                             final Throwable t
>                         )
>                     {
>                         logger.trace( message, t );
>                     }
>                 }
>             );
>     //@}
>     ///@name Fields
>     //@{
>         public final String name;
>         
>         private final ILevelHandler _level_handler;
>         
>     //@}
>     ///@name Lifetime Management
>     //@{
>         private LogLevel (
>                 final String name,
>                 final ILevelHandler handler
>             )
>         {
>             this.name = name;
>             
>             _level_handler = handler;
>         }
>     //@}
>     ///@name Accessors
>     //@{
>         /**
>          * @return name of this level
>          */
>         public String toString ( )
>         {
>             return name;
>         }
>         /**
>          * <p>Check whether this level is enabled in a given [EMAIL 
> PROTECTED] Log}
>          * passed as a parameter.</p>
>          * 
>          * @param logger
>          *
>          * @return boolean True if this level is enabled in
>          *      <code>logger</code>.
>          */
>         public boolean isEnabledIn ( final Log logger )
>         {
>             return _level_handler.isEnabledIn( logger );
>         }
>     //@}
>         
>     ///@name Methods
>     //@{
>         /**
>          * <p>Log a message with this log level to a given [EMAIL PROTECTED] 
> Log}
>          * passed as a parameter.</p>
>          *
>          * @param logger to where send this message
>          * @param message log this message
>          */
>         public void log (
>                 final Log logger,
>                 final Object message
>             )
>         {
>             _level_handler.log( logger, message );
>         }
>         
>         /**
>          * <p>Log an error with this log level to a given [EMAIL PROTECTED] 
> Log}
>          * passed as a parameter.</p>
>          *
>          * @param logger to where send this message
>          * @param message log this message
>          * @param t log this cause
>          */
>         public void log (
>                 final Log logger,
>                 final Object message,
>                 final Throwable t
>             )
>         {
>             _level_handler.log( logger, message, t );
>         }
>     //@}
>             
>     
>     private static interface ILevelHandler
>     {
>         boolean isEnabledIn ( final Log logger );
>         
>         void log (
>                 final Log logger,
>                 final Object message
>             );
>         
>         void log (
>                 final Log logger,
>                 final Object message,
>                 final Throwable t
>             );
>     }
> }
> ----------END OF LogLevel.java

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to