コード例 #1
0
  /**
   * 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)
    }
  }
コード例 #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);
   }
 }
コード例 #3
0
  void doTest(String filename, Integer expectedInt, Class<?> exceptionClass) throws Exception {

    rulesMap.put(new ElementSelector("test"), new NOPAction());
    rulesMap.put(new ElementSelector("test/badBegin"), new BadBeginAction());
    rulesMap.put(new ElementSelector("test/badBegin/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    rulesMap.put(new ElementSelector("test/isolate"), new NOPAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd"), new BadEndAction());
    rulesMap.put(new ElementSelector("test/isolate/badEnd/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/isolate/touch"), new TouchAction());
    rulesMap.put(new ElementSelector("test/hello"), new HelloAction());

    TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
    tc.setContext(context);
    tc.doConfigure(CoreTestConstants.TEST_SRC_PREFIX + "input/joran/skip/" + filename);

    String str = context.getProperty(HelloAction.PROPERTY_KEY);
    assertEquals("Hello John Doe.", str);

    Integer i = (Integer) context.getObject(TouchAction.KEY);
    if (expectedInt == null) {
      assertNull(i);
    } else {
      assertEquals(expectedInt, i);
    }

    // check the existence of an ERROR status
    List<Status> statusList = sm.getCopyOfStatusList();
    Status s0 = statusList.get(0);
    assertEquals(Status.ERROR, s0.getLevel());
    assertTrue(s0.getThrowable().getClass() == exceptionClass);
  }
コード例 #4
0
  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);
    }
  }
コード例 #5
0
 private void resetStatusListeners() {
   StatusManager sm = getStatusManager();
   for (StatusListener sl : sm.getCopyOfStatusListenerList()) {
     sm.remove(sl);
   }
 }