commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexander Pogrebnyak (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LOGGING-110) Implement a Level class and a generic log method in Log
Date Fri, 09 May 2008 15:52:55 GMT

    [ https://issues.apache.org/jira/browse/LOGGING-110?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12595650#action_12595650
] 

Alexander Pogrebnyak commented on LOGGING-110:
----------------------------------------------

I am transfering some notes here from the duplicate LOGGER-123 issue that I have created.

I have created and tested LogLevel class that does not require any changes to the current
Log interface ( it is all done through the inversion of control).
Therefore the LogLevel class can be dropped into a maintanance release at any point.

Here is a sample usage of the LogLevel object in implementing Command class

{code:title=Sample LogLevel Usage|borderStyle=solid}
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" );
    }
  }
}
{code}

For the next feature release 3 proposed methods ( isEnabled(), log(l, o), log(l,o,t) ) can
be added to the Log interface and implemented through the generic base class:

{code:title=LoggerBase.java|borderStyle=solid}
public abstract class LoggerBase implements Log
{
    public final boolean isEnabledFor (
            final LogLevel level
        )
    {
        return level.isEnabledIn( this );
    }
    
    public final void log (
            final LogLevel level,
            final Object message
        )
    {
        level.log( this, message );
    }
    
    public final void log (
            final LogLevel level,
            final Object message,
            final Throwable t
        )
    {
        level.log( this, message, t );
    }
}
{code}

Then all the supported loggers can extend from this base class and have these 3 methods defined
for them
{code:title=AvalonLogger.java|borderStyle=solid}
// currently "public class AvalonLogger implements Log"
public class AvalonLogger extends LoggerBase {
    ...
}
{code}

I am attaching the source code for LogLevel and a unit test for it.
The unit test is using JMock 1.1.0 framework for mocking Log interface.
Feel free to rewrite the test case with the mock framework of your choice.


> Implement a Level class and a generic log method in Log
> -------------------------------------------------------
>
>                 Key: LOGGING-110
>                 URL: https://issues.apache.org/jira/browse/LOGGING-110
>             Project: Commons Logging
>          Issue Type: New Feature
>            Reporter: Sebastiaan van Erk
>             Fix For: 2.0
>
>
> The Log API does not have a generic log method and there is no generic Level class. Since
the levels which commons logging provides are fixed and since it would not break backwards
compatibiliy I would like to suggest that these are added. To be more specific, I would like
to see the following methods added:
>  void 	log(Level level, Object message)
>            Log a message with the specified log level.
>  void 	log(Level level, Object message, Throwable t)
>            Log a message and exception with the specified log level.
>  boolean isEnabled(Level level)
>            Is the specified logging level currently enabled?
> As an extra feature of the level class one could have string and integer conversions
to and from log levels.
> These features would allow one to use commons logging in more complex situations without
have to rely on specific logging implementations.

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


Mime
View raw message