@Test
 public void testNotifyOnStopResistant() {
   listener.setResetResistant(true);
   context.stop();
   assertEquals(UpdateType.STOP, listener.updateType);
   assertEquals(listener.context, context);
 }
 @Test
 @Ignore
 // this test works only if logback-test.xml or logback.xml files are on the classpath.
 // However, this is something we try to avoid in order to simplify the life
 // of users trying to follow the manual and logback-examples from an IDE
 public void reset() throws JoranException {
   {
     new ContextInitializer(loggerContext).autoConfig();
     Appender<ILoggingEvent> appender = root.getAppender("STDOUT");
     assertNotNull(appender);
     assertTrue(appender instanceof ConsoleAppender);
   }
   {
     loggerContext.stop();
     Appender<ILoggingEvent> appender = root.getAppender("STDOUT");
     assertNull(appender);
   }
 }
Esempio n. 3
0
  static void perfCase(boolean safetyMode) throws Exception {
    LoggerContext lc = buildLoggerContext(FILENAME + "-" + safetyMode + ".log", safetyMode);
    Logger logger = lc.getLogger(FileAppenderPerf.class);

    long start = System.nanoTime();
    for (int i = 0; i < LEN; i++) {
      logger.debug(msgLong + " " + i);
    }
    // in microseconds
    double durationPerLog = (System.nanoTime() - start) / (LEN * 1000.0);

    lc.stop();

    System.out.println(
        "Average duration of "
            + (durationPerLog)
            + " microseconds per log. Prudent mode="
            + safetyMode);
    System.out.println("------------------------------------------------");
  }
 @Test
 public void testNotifyOnStopNotResistant() {
   context.stop();
   assertEquals(UpdateType.RESET, listener.updateType);
   assertEquals(listener.context, context);
 }
Esempio n. 5
0
 /**
  * This method must be called when the application is getting closed/destroyed. In order to
  * release the resources used by logback-classic, it is always a good idea to stop the logback
  * context. Stopping the context will stop all appenders attached to loggers defined by the
  * context and stop any active threads. This mean all unprocessed events in the appender's queue
  * will get discarded. Hence if developers don't want their log events to get discarded then they
  * need to make sure that they call this method only when all the events in the queue are
  * processed by appenders (SocketAppender to be more precisely, which is slow is processing events
  * as it has to sent them over wire to central log server). To achieve this just call this method
  * after giving a appropriate sleep/delay duration. If losing unprocessed events is not important
  * then this can be called without any sleep/delay.
  */
 public static void stopLoggerContext() {
   LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
   loggerContext.stop();
 }
Esempio n. 6
0
  @SuppressWarnings("unchecked")
  public static void main(String... args) throws ParseException, JoranException, IOException {
    Parser parser = new BasicParser();
    CommandLine cl = null;
    try {
      cl = parser.parse(OPTS, args);
    } catch (ParseException e) {
      HelpFormatter help = new HelpFormatter();
      help.printHelp("dlog", OPTS, true);
      System.exit(-1);
    }

    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.reset();

    if (cl.hasOption("config")) {
      // Read Logback configuration
      JoranConfigurator configurator = new JoranConfigurator();
      configurator.setContext(loggerContext);

      configurator.doConfigure(cl.getOptionValue("file", "logback.xml"));

      StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
    } else {
      BasicConfigurator.configure(loggerContext);
    }

    Appender appender = null;
    if (cl.hasOption("output")) {
      String outputAppender = cl.getOptionValue("output", "console");
      appender = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).getAppender(outputAppender);
    }

    ChronicleTools.warmup();
    Chronicle chronicle = new IndexedChronicle(cl.getOptionValue("path"), ChronicleConfig.DEFAULT);
    ExcerptTailer ex = chronicle.createTailer();

    Level level = Level.valueOf(cl.getOptionValue("level", "TRACE"));

    if (cl.hasOption("head")) {
      int lines = Integer.parseInt(cl.getOptionValue("head", "10"));
      for (int i = 0; i < lines; i++) {
        LoggingEvent evt = readLoggingEvent(ex, loggerContext);
        if (evt.getLevel().isGreaterOrEqual(level)) {
          writeEvent(evt, appender);
        }
      }
    } else if (cl.hasOption("tail")) {
      int lines = Integer.parseInt(cl.getOptionValue("tail", "10"));
      Queue<LoggingEvent> tail = new LinkedBlockingQueue<LoggingEvent>(lines);
      while (ex.nextIndex()) {
        LoggingEvent evt = readLoggingEvent(ex, loggerContext);
        if (!tail.offer(evt)) {
          tail.poll();
          tail.add(evt);
        }
      }
      LoggingEvent evt;
      while (null != (evt = tail.poll())) {
        if (evt.getLevel().isGreaterOrEqual(level)) {
          writeEvent(evt, appender);
        }
      }
    } else if (cl.hasOption("search")) {
      String regex = cl.getOptionValue("search");
      Pattern regexPatt = Pattern.compile(regex);
      while (ex.nextIndex()) {
        LoggingEvent evt = readLoggingEvent(ex, loggerContext);
        if (null != evt && evt.getLevel().isGreaterOrEqual(level)) {
          if (regexPatt.matcher(evt.getFormattedMessage()).matches()) {
            writeEvent(evt, appender);
          }
        }
      }
    }

    loggerContext.stop();
    chronicle.close();
  }
 /**
  * Shut down logback, properly releasing all file locks.
  *
  * <p>This isn't strictly necessary, but recommended for shutting down logback in a scenario where
  * the host VM stays alive (for example, when shutting down an application in a J2EE environment).
  */
 public static void shutdownLogging() {
   lc.stop();
 }
 /**
  * Shut down logback.
  *
  * <p>This isn't strictly necessary, but recommended for shutting down logback in a scenario where
  * the host VM stays alive (for example, when shutting down an application in a J2EE environment).
  */
 public static void shutdownLogging() {
   LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
   loggerContext.stop();
 }
 private void checkThatTaskCanBeStopped() {
   ScheduledFuture<?> future = loggerContext.getScheduledFutures().get(0);
   loggerContext.stop();
   assertTrue(future.isCancelled());
 }