/**
   * Initialize log level overrides from debug options.
   *
   * <p>This may only be called during bootstrapping before any custom overrides are set. Your
   * milage may vary if called while the application is running.
   *
   * @throws Exception
   */
  void initializeLogLevelOverrides() throws Exception {
    // reset current overrides
    overriddenLogLevels.clear();

    // add a note to the status manager
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final StatusManager sm = lc.getStatusManager();
    if (sm != null) {
      sm.add(new InfoStatus("Initializing log level overrides.", this));
    }

    // apply new overrides
    try {
      final Map<String, String> options =
          BootActivator.getInstance().getService(DebugOptions.class).getOptions();
      for (final Entry<String, String> e : options.entrySet()) {
        final String loggerName = getLoggerNameForDebugOption(e.getKey());
        if (loggerName != null) {
          if ((null != e.getValue()) && !"false".equalsIgnoreCase(e.getValue())) {
            setLogLevelOverride(loggerName, "DEBUG");
          }
        }
      }
    } catch (final ServiceNotAvailableException e) {
      // no debug options available (ignore)
    }
  }
Exemplo n.º 2
0
 public void addStatus(Status status) {
   if (context == null) {
     if (noContextWarning++ == 0) {
       System.out.println("LOGBACK: No context given for " + this);
     }
     return;
   }
   StatusManager sm = context.getStatusManager();
   if (sm != null) {
     sm.add(status);
   }
 }
  void setLogLevelOverride(final String loggerName, final String level) throws Exception {
    // get logger
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final StatusManager sm = lc.getStatusManager();
    final Logger logger = lc.getLogger(loggerName);

    // get levels
    final Level currentLevel = logger.getLevel();
    final Level newLevel = Level.toLevel(level, null);

    if (newLevel == null) {
      // reset any current override
      final String[] removed = overriddenLogLevels.remove(loggerName);
      if (removed != null) {
        final Level toRestore = Level.toLevel(removed[1], null);
        if (sm != null) {
          sm.add(
              new InfoStatus(
                  String.format(
                      "Resetting level for logger '%s'.", loggerName, logger.getEffectiveLevel()),
                  this));
        }
        logger.setLevel(toRestore);
      }
    } else if (newLevel != currentLevel) {
      // apply new override
      overriddenLogLevels.put(
          loggerName, new String[] {level, null != currentLevel ? currentLevel.levelStr : null});
      if (sm != null) {
        sm.add(
            new InfoStatus(
                String.format("Overriding level for logger '%s' to '%s'.", loggerName, newLevel),
                this));
      }
      logger.setLevel(newLevel);
    }
  }