On Tue, Aug 5, 2008 at 9:10 AM, Emmanuel Lecharny <[EMAIL PROTECTED]> wrote:
> The ultimate optimization, considering that the logs will become static,
> would be to add a static with an evaluation of the log state :
>
>   private static final Logger LOG = LoggerFactory.getLogger( <your
> class>.class );
>
>   /**
>    * Speedup for logs
>    */
>   private static final boolean IS_DEBUG = LOG.isDebugEnabled();
> ...
>   if ( IS_DEBUG ) {
>       LOG.debug( blah );
>   }
>
> Doing so will allow the JVM to simply remove the test, as if DEBUG is not
> true, and as it's static, the optimizer will consider that the inner code
> will never be executed.

I would strongly lean aginst doing this. I'm frequently using logging
in dynamic environments (e.g. using the Apache Felix web console)
where logging is turned on and off without a JVM restart. This type of
code would not work in that type of environment.

On isXXXEnabled(), note that SLF4J will do this check on it's own, for
example, this is from the Log4jLoggerAdapter class:

  public void debug(String format, Object[] argArray) {
    if (logger.isDebugEnabled()) {
      String msgStr = MessageFormatter.arrayFormat(format, argArray);
      logger.log(FQCN, Level.DEBUG, msgStr, null);
    }
  }

So, wrapping a log call within the isXXXEnabled() will actually make
logging slower in the case where it is enabled. In the case where it
is disabled, the extra cost will only be the method invocation of the
debug() (or similar) method. Not that this speed up or slow down will
matter in most cases anyways :-)

Also, while on the topic, having static loggers might not be such a
good idea, see this discussion:
http://wiki.apache.org/jakarta-commons/Logging/StaticLog

/niklas

Reply via email to