Java 类com.codahale.metrics.logback.InstrumentedAppender 实例源码

项目:springJpaKata    文件:MetricsConfig.java   
@Override
public void configureReporters(MetricRegistry metricRegistry) {
    final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME);

    final InstrumentedAppender metrics = new InstrumentedAppender(metricRegistry);
    metrics.setContext(root.getLoggerContext());
    metrics.start();
    root.addAppender(metrics);
    JmxReporter.forRegistry(metricRegistry).build().start();
    Slf4jReporter reporter = Slf4jReporter.forRegistry(metricRegistry)
            .outputTo(LoggerFactory.getLogger("metrics"))
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
            reporter.start(1, TimeUnit.MINUTES);
}
项目:fili    文件:ResourceConfig.java   
/**
 * Create a Codahale metric appender and add to ROOT logger.
 */
private void registerMetricsAppender() {
    // Get the root logger
    LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger rootLogger = factory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);

    // Create the metrics appender and give it the root logger's context
    InstrumentedAppender appender = new InstrumentedAppender(MetricRegistryFactory.getRegistry());
    appender.setContext(rootLogger.getLoggerContext());
    appender.start();

    // Add the appender to the root logger
    rootLogger.addAppender(appender);
}
项目:fili    文件:JerseyTestBinder.java   
/**
 * Create a Codahale metric appender and add to ROOT logger.
 */
private void registerMetricsAppender() {
    // Get the root logger
    LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
    ch.qos.logback.classic.Logger rootLogger = factory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

    // Create the metrics appender and give it the root logger's context
    InstrumentedAppender appender = new InstrumentedAppender(MetricRegistryFactory.getRegistry());
    appender.setContext(rootLogger.getLoggerContext());
    appender.start();

    // Add the appender to the root logger
    rootLogger.addAppender(appender);
}
项目:cgif    文件:Global.java   
private void setupMetrics(Configuration configuration) {
  boolean metricsJvm     = configuration.getBoolean("metrics.jvm", false);
  boolean metricsLogback = configuration.getBoolean("metrics.logback", false);
  boolean metricsConsole = configuration.getBoolean("metrics.console", false);

  if(metricsJvm) {
    metricRegistry.registerAll(new GarbageCollectorMetricSet());
    metricRegistry.registerAll(new MemoryUsageGaugeSet());
    metricRegistry.registerAll(new ThreadStatesGaugeSet());
  }

  if (metricsLogback) {
    InstrumentedAppender appender = new InstrumentedAppender(metricRegistry);

    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)Logger.underlying();
    appender.setContext(logger.getLoggerContext());
    appender.start();
    logger.addAppender(appender);
  }

  if (metricsConsole) {
    ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry)
      .convertRatesTo(TimeUnit.SECONDS)
      .convertDurationsTo(TimeUnit.MILLISECONDS)
      .build();
    consoleReporter.start(1, TimeUnit.SECONDS);
  }

}
项目:baleen    文件:BaleenLogging.java   
/**
 * 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);
}
项目:chassis    文件:ChassisConfiguration.java   
/**
 * 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;
}
项目:baleen    文件:BaleenLoggingTest.java   
@Test
public void config() throws Exception {
    YamlConfiguration configuration = YamlConfiguration.readFromResource(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
}