[slf4j-user] Can/should slf4j handle Guice AOP class names?

planet57 jason at planet57.com
Tue Oct 29 01:32:58 CET 2013


I’m wondering if perhaps slf4j could/should handle unwrapping Guice AOP _enhanced_ classes when creating Logger instances?

Some code simply can not use static class references and has to use obj.getClass() to configure a Logger instance.  This can however result in ugly Guice turds in the logger name if the class was enhanced, for example:

com.whatever.EventPublisherImpl$$EnhancerByGuice$$

… instead of what is generally preferable:

com.whatever.EventPublisherImpl

The AOP stuff here is an impl detail and IMO should leak out into logging context.

I’m wondering if there is a simple/elegant way to handle this in the slf4j-api (and become generally transparent to applications) or if the only good option is to leave it up to applications and then use a custom factory facade like this to unwrap:

<snip>
public class Loggers
{
  private static final String GUICE_ENHANCED = "$$EnhancerByGuice$$";

  private static boolean isEnhancedSubclass(final Class type) {
    return type.getName().endsWith(GUICE_ENHANCED);
  }

  public static Logger getLogger(final Class type) {
    checkNotNull(type);
    if (isEnhancedSubclass(type)) {
      return LoggerFactory.getLogger(type.getSuperclass());
    }
    return LoggerFactory.getLogger(type);
  }

  public static Logger getLogger(final Object obj) {
    checkNotNull(obj);
    return getLogger(obj.getClass());
  }

  public static Logger getLogger(final String name) {
    return LoggerFactory.getLogger(name);
  }
}
</snip>

I had considered some sort of magic trickery by adding a facade static binding and then somehow delegating to a real binding, but all of that sounds just too complex.

Anyone have any thoughts?

—jason

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/slf4j-user/attachments/20131028/e0a5e4df/attachment.html>


More information about the slf4j-user mailing list