Search in sources :

Example 31 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project dropwizard by dropwizard.

the class AppenderFactoryCustomTimeZoneTest method testBuildAppenderWithTimeZonePlaceholderInLogFormat.

@Test
void testBuildAppenderWithTimeZonePlaceholderInLogFormat() throws Exception {
    Appender appender = factory.build(configurationSourceProvider, "yaml/appender_with_time_zone_placeholder.yml").build(new LoggerContext(), "test-custom-time-zone", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    assertThat(appender).isInstanceOfSatisfying(AsyncAppender.class, asyncAppender -> assertThat(asyncAppender.getAppender("console-appender")).isInstanceOfSatisfying(ConsoleAppender.class, consoleAppender -> assertThat(consoleAppender.getEncoder()).isInstanceOfSatisfying(LayoutWrappingEncoder.class, encoder -> assertThat(encoder.getLayout()).isInstanceOfSatisfying(PatternLayoutBase.class, layout -> assertThat(layout.getPattern()).isEqualTo("custom format with UTC")))));
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Appender(ch.qos.logback.core.Appender) AsyncAppender(ch.qos.logback.classic.AsyncAppender) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) CsvSource(org.junit.jupiter.params.provider.CsvSource) ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) TimeZone(java.util.TimeZone) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResourceConfigurationSourceProvider(io.dropwizard.configuration.ResourceConfigurationSourceProvider) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Appender(ch.qos.logback.core.Appender) Jackson(io.dropwizard.jackson.Jackson) AsyncAppender(ch.qos.logback.classic.AsyncAppender) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) BaseValidator(io.dropwizard.validation.BaseValidator) PatternLayoutBase(ch.qos.logback.core.pattern.PatternLayoutBase) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) PatternLayoutBase(ch.qos.logback.core.pattern.PatternLayoutBase) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 32 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project dropwizard by dropwizard.

the class BootstrapLogging method bootstrap.

public static void bootstrap(Level level, DiscoverableLayoutFactory<ILoggingEvent> layoutFactory) {
    LoggingUtil.hijackJDKLogging();
    BOOTSTRAPPING_LOCK.lock();
    try {
        if (bootstrapped) {
            return;
        }
        final Logger root = LoggingUtil.getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        root.detachAndStopAllAppenders();
        final Layout<ILoggingEvent> layout = layoutFactory.build(root.getLoggerContext(), TimeZone.getDefault());
        layout.start();
        final ThresholdFilter filter = new ThresholdFilter();
        filter.setLevel(level.toString());
        filter.start();
        final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
        appender.addFilter(filter);
        appender.setContext(root.getLoggerContext());
        final LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
        layoutEncoder.setLayout(layout);
        appender.setEncoder(layoutEncoder);
        appender.start();
        root.addAppender(appender);
        bootstrapped = true;
    } finally {
        BOOTSTRAPPING_LOCK.unlock();
    }
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) ThresholdFilter(ch.qos.logback.classic.filter.ThresholdFilter) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder)

Example 33 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project dropwizard by dropwizard.

the class DefaultLoggingFactory method reset.

@Override
public void reset() {
    CHANGE_LOGGER_CONTEXT_LOCK.lock();
    try {
        // Flush all the loggers and reinstate only the console logger as a
        // sane default.
        loggerContext.stop();
        final Logger logger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        logger.detachAndStopAllAppenders();
        final DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault());
        formatter.start();
        final LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
        layoutEncoder.setLayout(formatter);
        final ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
        consoleAppender.addFilter(new ThresholdLevelFilterFactory().build(Level.INFO));
        consoleAppender.setEncoder(layoutEncoder);
        consoleAppender.setContext(loggerContext);
        consoleAppender.start();
        logger.addAppender(consoleAppender);
        loggerContext.start();
    } finally {
        CHANGE_LOGGER_CONTEXT_LOCK.unlock();
    }
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LayoutWrappingEncoder(ch.qos.logback.core.encoder.LayoutWrappingEncoder) ThresholdLevelFilterFactory(io.dropwizard.logging.filter.ThresholdLevelFilterFactory)

Example 34 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project dropwizard by dropwizard.

the class DefaultLoggingFactoryTest method testResetAppenders.

@Test
void testResetAppenders() throws Exception {
    final DefaultLoggingFactory config = factory.build(configurationSourceProvider, "yaml/logging.yml");
    config.configure(new MetricRegistry(), "test-logger");
    config.reset();
    // There should be exactly one appender configured, a ConsoleAppender
    assertThat(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).isInstanceOfSatisfying(Logger.class, logger -> assertThat(logger.iteratorForAppenders()).toIterable().singleElement().isInstanceOf(ConsoleAppender.class).as("context").matches((Appender<?> a) -> a.getContext() != null).as("started").matches(LifeCycle::isStarted));
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.jupiter.api.Test)

Example 35 with ConsoleAppender

use of ch.qos.logback.core.ConsoleAppender in project midpoint by Evolveum.

the class Log method init.

private void init() {
    opts = NinjaUtils.getOptions(context.getJc(), BaseOptions.class);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    if (opts.isVerbose()) {
        ple.setPattern("%date [%thread] %-5level \\(%logger{46}\\): %message%n<");
    } else {
        ple.setPattern("%msg%n");
    }
    ple.setContext(lc);
    ple.start();
    ConsoleAppender out = setupAppender("STDOUT", "System.out", lc, setupEncoder(lc));
    ConsoleAppender err = setupAppender("STDERR", "System.err", lc, setupEncoder(lc));
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    if (LogTarget.SYSTEM_OUT.equals(target)) {
        root.addAppender(out);
    } else {
        root.addAppender(err);
    }
    root.setLevel(Level.OFF);
    info = setupLogger(LOGGER_SYS_OUT, opts);
    error = setupLogger(LOGGER_SYS_ERR, opts);
    error.setAdditive(false);
    error.addAppender(err);
}
Also used : BaseOptions(com.evolveum.midpoint.ninja.opts.BaseOptions) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

ConsoleAppender (ch.qos.logback.core.ConsoleAppender)38 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)31 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)21 LoggerContext (ch.qos.logback.classic.LoggerContext)19 Logger (ch.qos.logback.classic.Logger)16 Appender (ch.qos.logback.core.Appender)10 FileAppender (ch.qos.logback.core.FileAppender)9 Test (org.junit.Test)8 ThresholdFilter (ch.qos.logback.classic.filter.ThresholdFilter)5 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)5 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)5 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)4 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)2 JoranException (ch.qos.logback.core.joran.spi.JoranException)2 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 PmsConfiguration (net.pms.configuration.PmsConfiguration)2