Uploaded image for project: 'SLF4J'
  1. SLF4J
  2. SLF4J-283

Custom JUL-LogManager to support multiple configurations (Logback).

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • None
    • 1.7.x
    • jul-to-slf4j
    • None
    • Operating System: Windows XP
      Platform: PC

    Description

      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); }

      }

      // ...
      }

      Attachments

        Activity

          People

            slf4j-dev SLF4J developers list
            thomoell Thomas Möller
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: