com.codahale.metrics.logback.InstrumentedAppender Java Examples

The following examples show how to use com.codahale.metrics.logback.InstrumentedAppender. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaleenLogging.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Configure logging based on a list of builders provided to it. Injects the configured logging to
 * replace the default UIMA loggers, and also sets up metrics on the logging.
 *
 * @param builders The builders to use to configure the logging
 */
public void configure(List<BaleenLoggerBuilder> builders) {
  // Install JUL to SLF4J handling (JUL is default for UIMA)
  SLF4JBridgeHandler.removeHandlersForRootLogger();
  SLF4JBridgeHandler.install();

  // Configure Logback
  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

  // Install the level change propagator to reduce the impact of JUL logging
  context.addListener(new LevelChangePropagator());

  // Remove all the existing appenders
  rootLogger.detachAndStopAllAppenders();

  for (BaleenLoggerBuilder builder : builders) {
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setCharset(StandardCharsets.UTF_8);
    ple.setContext(context);
    ple.setPattern(builder.getPattern());
    ple.start();

    Appender<ILoggingEvent> appender = builder.build(context, ple);
    if (!appender.isStarted()) {
      appender.start();
    }

    rootLogger.addAppender(appender);
  }

  LOGGER.debug("Adding instrumented metrics for logging");
  // Add an instrumented appender so we get the information about logging
  // through metrics
  InstrumentedAppender instrumentedAppender =
      new InstrumentedAppender(MetricsFactory.getInstance().getRegistry());
  instrumentedAppender.setContext(context);
  instrumentedAppender.start();
  rootLogger.addAppender(instrumentedAppender);
}
 
Example #2
Source File: ChassisConfiguration.java    From chassis with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the metrics registry
 *
 * @return metric registry bean
 */
@Bean
public MetricRegistry metricRegistry() {
    final MetricRegistry bean = new MetricRegistry();

    // add JVM metrics
    bean.register("jvm.gc", new GarbageCollectorMetricSet());
    bean.register("jvm.memory", new MemoryUsageGaugeSet());
    bean.register("jvm.thread-states", new ThreadStatesGaugeSet());
    bean.register("jvm.fd", new FileDescriptorRatioGauge());
    bean.register("jvm.load-average", new Gauge<Double>() {
        private OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();

        public Double getValue() {
            try {
                return mxBean.getSystemLoadAverage();
            } catch (Exception e) {
                // not supported
                return -1d;
            }
        }
    });

    // add Logback metrics
    final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME);
    final InstrumentedAppender appender = new InstrumentedAppender(bean);
    appender.setContext(root.getLoggerContext());
    appender.start();
    root.addAppender(appender);

    return bean;
}
 
Example #3
Source File: BaleenLoggingTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void config() throws Exception {
  YamlConfiguration configuration =
      new YamlConfiguration(BaleenLoggingTest.class, "dummyConfig.yaml");

  BaleenLogging logging = new BaleenLogging();
  logging.configure(configuration);

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

  int count = 0;

  Iterator<Appender<ILoggingEvent>> it = rootLogger.iteratorForAppenders();
  while (it.hasNext()) {
    Appender<ILoggingEvent> appender = it.next();

    switch (count) {
      case 0:
        assertTrue(appender instanceof ConsoleAppender);
        break;
      case 1:
        assertTrue(appender instanceof RollingFileAppender);
        break;
      case 2:
        assertTrue(appender instanceof FileAppender);
        assertFalse(appender instanceof RollingFileAppender);
        break;
      case 3:
        if (appender instanceof OutputStreamAppender) {
          Encoder<ILoggingEvent> e =
              ((OutputStreamAppender<ILoggingEvent>) appender).getEncoder();
          assertTrue(e instanceof PatternLayoutEncoder);
          assertEquals(PATTERN, ((PatternLayoutEncoder) e).getPattern());
        }
        break;
      case 4:
        if (appender instanceof EvictingQueueAppender) {
          assertEquals(
              EvictingQueueAppender.DEFAULT_MAX_SIZE,
              ((EvictingQueueAppender<ILoggingEvent>) appender).getMaxSize());
        } else {
          fail("Unknown additional appender");
        }
        break;
      case 5:
        // Allow additional appenders for checking, otherwise throw an error
        if (!(appender instanceof InstrumentedAppender)) {
          fail("Unknown additional appender");
        }
        break;
      default:
        fail("Too many appenders" + appender.getName());
    }
    count++;
  }

  assertEquals(6, count);

  // TODO: test the instance parameters match the configuration
}