[slf4j-dev] [Bug 292] New: Custom JUL-LogManager to support multiple configurations (Logback).

bugzilla-daemon at qos.ch bugzilla-daemon at qos.ch
Tue Feb 19 14:08:21 CET 2013


http://bugzilla.slf4j.org/show_bug.cgi?id=292

           Summary: Custom JUL-LogManager to support multiple
                    configurations (Logback).
           Product: SLF4J
           Version: 1.7.x
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: jul-to-slf4j
        AssignedTo: slf4j-dev at qos.ch
        ReportedBy: thomas.moeller at continentale.de


I suppose to implement a custom JUL-LogManager that switches off the JUL
configuration completely:

public class Slf4jLogManager extends LogManager
{
  public Slf4jLogManager()
  {
  }

  @Override
  public void readConfiguration() throws IOException, SecurityException
  {
  }

  @Override
  public void readConfiguration(InputStream ins) throws IOException,
      SecurityException
  {
  }

  @Override
  public synchronized boolean addLogger(Logger logger)
  {
    return super.addLogger(new Slf4jLogger(logger.getName(), logger
        .getResourceBundleName()));
  }
}

public class Slf4jLoggerAdapter
{
  private final String name;
  private volatile Logger logger;

  public Slf4jLoggerAdapter(String name)
  {
    this.name = name;
  }

  private Logger resolve()
  {
    Logger logger = this.logger;
    if (logger == null)
    {
      logger = demandLogger();
      this.logger = logger;
    }
    return logger;
  }

  private Logger demandLogger()
  {
    return LoggerFactory.getLogger(name);
  }

  public Level getLevel()
  {
    org.slf4j.Logger logger = resolve();
    Level level;
    if (logger.isTraceEnabled())
    {
      level = Level.FINEST;
    }
    else if (logger.isDebugEnabled())
    {
      level = Level.FINER;
    }
    else if (logger.isInfoEnabled())
    {
      level = Level.INFO;
    }
    else if (logger.isWarnEnabled())
    {
      level = Level.WARNING;
    }
    else if (logger.isErrorEnabled())
    {
      level = Level.SEVERE;
    }
    else
    {
      level = Level.OFF;
    }
    return level;
  }

  public boolean isLoggable(Level level)
  {
    return getLevel().intValue() <= level.intValue();
  }

  public boolean isSevereEnabled()
  {
    return resolve().isErrorEnabled();
  }

  // ...
}

public class Slf4jLogger extends Logger
{
  private static final Handler BRIDGE_HANDLER = new SLF4JBridgeHandler();

  private final Slf4jLoggerAdapter adapter;

  public Slf4jLogger(String name, String resourceBundleName)
  {
    super(name, resourceBundleName);
    adapter = new Slf4jLoggerAdapter(name);
    super.setLevel(Level.ALL);
    super.addHandler(BRIDGE_HANDLER);
    super.setUseParentHandlers(false);
  }

  @Override
  public Level getLevel()
  {
    return adapter.getLevel();
  }

  @Override
  public boolean isLoggable(Level level)
  {
    return adapter.isLoggable(level);
  }

  @Override
  public void severe(String msg)
  {
    if (adapter.isSevereEnabled())
    {
      super.severe(msg);
    }
  }

  // ...
}

-- 
Configure bugmail: http://bugzilla.slf4j.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the slf4j-dev mailing list