Search in sources :

Example 71 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project openhab1-addons by openhab.

the class LoggingPersistenceService method createNewAppender.

protected FileAppender<ILoggingEvent> createNewAppender(String alias) {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(pattern);
    encoder.start();
    FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
    appender.setAppend(true);
    appender.setFile(LOG_FOLDER + File.separator + alias + LOG_FILEEXT);
    appender.setEncoder(encoder);
    appender.setContext(context);
    appender.start();
    return appender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FileAppender(ch.qos.logback.core.FileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 72 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project chassis by Kixeye.

the class AppMain method initializeLogging.

private static void initializeLogging(Arguments arguments) {
    //need to exclude logback from their project dependencies and initialize their own logging
    try {
        AppMain.class.getClassLoader().loadClass("ch.qos.logback.classic.LoggerContext");
    } catch (ClassNotFoundException e) {
        System.err.println("No Logback found in classpath. Skipping logging initialization. This is expected if you are configuring the logging system yourself. If you are not configuring logging yourself and would like to use logging provided by java-bootstrap, ensure that the Logback dependency jar exists in your classpath.");
        return;
    }
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    BasicConfigurator.configure(context);
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    Level level = Level.valueOf(arguments.logLevel.name().toUpperCase());
    logger.setLevel(level);
    logger.info("Initialized logging at {} level", level);
}
Also used : Level(ch.qos.logback.classic.Level) Logger(org.slf4j.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 73 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project chassis by Kixeye.

the class ChassisConfiguration method metricRegistry.

/**
     * 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;
}
Also used : InstrumentedAppender(com.codahale.metrics.logback.InstrumentedAppender) FileDescriptorRatioGauge(com.codahale.metrics.jvm.FileDescriptorRatioGauge) MemoryUsageGaugeSet(com.codahale.metrics.jvm.MemoryUsageGaugeSet) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(ch.qos.logback.classic.Logger) ThreadStatesGaugeSet(com.codahale.metrics.jvm.ThreadStatesGaugeSet) LoggerContext(ch.qos.logback.classic.LoggerContext) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) GarbageCollectorMetricSet(com.codahale.metrics.jvm.GarbageCollectorMetricSet) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Bean(org.springframework.context.annotation.Bean)

Example 74 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project chassis by Kixeye.

the class FlumeLoggerLoader method installFlumeAppender.

public synchronized void installFlumeAppender() {
    LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    appender.setContext(logContext);
    appender.start();
    Logger logger = logContext.getLogger(Logger.ROOT_LOGGER_NAME);
    if (logger.getAppender(appender.getName()) == null) {
        logger.addAppender(appender);
    }
}
Also used : Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 75 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project chassis by Kixeye.

the class FlumeLoggerLoader method uninstallFlumeAppender.

public synchronized void uninstallFlumeAppender() {
    LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    appender.setContext(logContext);
    appender.stop();
    Logger logger = logContext.getLogger(Logger.ROOT_LOGGER_NAME);
    if (logger.getAppender(appender.getName()) != null) {
        logger.detachAppender(appender);
    }
}
Also used : Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

LoggerContext (ch.qos.logback.classic.LoggerContext)216 Test (org.junit.Test)134 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)43 Logger (ch.qos.logback.classic.Logger)41 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)26 FileAppender (ch.qos.logback.core.FileAppender)22 File (java.io.File)21 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)19 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)16 Appender (ch.qos.logback.core.Appender)14 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)14 JoranException (ch.qos.logback.core.joran.spi.JoranException)14 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)13 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)12 AsyncAppender (ch.qos.logback.classic.AsyncAppender)11 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)11 Logger (org.slf4j.Logger)10 IOException (java.io.IOException)8 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)7 URL (java.net.URL)7