Search in sources :

Example 6 with DropwizardLayoutFactory

use of io.dropwizard.logging.layout.DropwizardLayoutFactory in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method includesCallerData.

@Test
public void includesCallerData() {
    FileAppenderFactory<ILoggingEvent> fileAppenderFactory = new FileAppenderFactory<>();
    fileAppenderFactory.setArchive(false);
    AsyncAppender asyncAppender = (AsyncAppender) fileAppenderFactory.build(new LoggerContext(), "test", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    assertThat(asyncAppender.isIncludeCallerData()).isFalse();
    fileAppenderFactory.setIncludeCallerData(true);
    asyncAppender = (AsyncAppender) fileAppenderFactory.build(new LoggerContext(), "test", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    assertThat(asyncAppender.isIncludeCallerData()).isTrue();
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppender(ch.qos.logback.classic.AsyncAppender) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 7 with DropwizardLayoutFactory

use of io.dropwizard.logging.layout.DropwizardLayoutFactory in project dropwizard by dropwizard.

the class DefaultLoggingFactory method configureLoggers.

private Logger configureLoggers(String name) {
    final Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    loggerContext.reset();
    final LevelChangePropagator propagator = new LevelChangePropagator();
    propagator.setContext(loggerContext);
    propagator.setResetJUL(true);
    loggerContext.addListener(propagator);
    root.setLevel(level);
    final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new ThresholdLevelFilterFactory();
    final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
    final LayoutFactory<ILoggingEvent> layoutFactory = new DropwizardLayoutFactory();
    for (Map.Entry<String, JsonNode> entry : loggers.entrySet()) {
        final Logger logger = loggerContext.getLogger(entry.getKey());
        final JsonNode jsonNode = entry.getValue();
        if (jsonNode.isTextual()) {
            // Just a level as a string
            logger.setLevel(Level.valueOf(jsonNode.asText()));
        } else if (jsonNode.isObject()) {
            // A level and an appender
            final LoggerConfiguration configuration;
            try {
                configuration = Jackson.newObjectMapper().treeToValue(jsonNode, LoggerConfiguration.class);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException("Wrong format of logger '" + entry.getKey() + "'", e);
            }
            logger.setLevel(configuration.getLevel());
            logger.setAdditive(configuration.isAdditive());
            for (AppenderFactory<ILoggingEvent> appender : configuration.getAppenders()) {
                logger.addAppender(appender.build(loggerContext, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
            }
        } else {
            throw new IllegalArgumentException("Unsupported format of logger '" + entry.getKey() + "'");
        }
    }
    return root;
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppenderFactory(io.dropwizard.logging.async.AsyncAppenderFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ThresholdLevelFilterFactory(io.dropwizard.logging.filter.ThresholdLevelFilterFactory) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) LevelChangePropagator(ch.qos.logback.classic.jul.LevelChangePropagator) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 8 with DropwizardLayoutFactory

use of io.dropwizard.logging.layout.DropwizardLayoutFactory in project dropwizard by dropwizard.

the class DefaultLoggingFactory method configure.

@Override
public void configure(MetricRegistry metricRegistry, String name) {
    LoggingUtil.hijackJDKLogging();
    CHANGE_LOGGER_CONTEXT_LOCK.lock();
    final Logger root;
    try {
        root = configureLoggers(name);
    } finally {
        CHANGE_LOGGER_CONTEXT_LOCK.unlock();
    }
    final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new ThresholdLevelFilterFactory();
    final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
    final LayoutFactory<ILoggingEvent> layoutFactory = new DropwizardLayoutFactory();
    for (AppenderFactory<ILoggingEvent> output : appenders) {
        root.addAppender(output.build(loggerContext, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
    }
    StatusPrinter.setPrintStream(configurationErrorsStream);
    try {
        StatusPrinter.printIfErrorsOccured(loggerContext);
    } finally {
        StatusPrinter.setPrintStream(System.out);
    }
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    MBEAN_REGISTRATION_LOCK.lock();
    try {
        final ObjectName objectName = new ObjectName("io.dropwizard:type=Logging");
        if (!server.isRegistered(objectName)) {
            server.registerMBean(new JMXConfigurator(loggerContext, server, objectName), objectName);
        }
    } catch (MalformedObjectNameException | InstanceAlreadyExistsException | NotCompliantMBeanException | MBeanRegistrationException e) {
        throw new RuntimeException(e);
    } finally {
        MBEAN_REGISTRATION_LOCK.unlock();
    }
    configureInstrumentation(root, metricRegistry);
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ThresholdLevelFilterFactory(io.dropwizard.logging.filter.ThresholdLevelFilterFactory) ObjectName(javax.management.ObjectName) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) JMXConfigurator(ch.qos.logback.classic.jmx.JMXConfigurator) MBeanRegistrationException(javax.management.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer)

Example 9 with DropwizardLayoutFactory

use of io.dropwizard.logging.layout.DropwizardLayoutFactory in project dropwizard by dropwizard.

the class ConsoleAppenderFactoryTest method appenderNameIsSet.

@Test
public void appenderNameIsSet() throws Exception {
    final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    final ConsoleAppenderFactory<ILoggingEvent> appenderFactory = new ConsoleAppenderFactory<>();
    final Appender<ILoggingEvent> appender = appenderFactory.build(root.getLoggerContext(), "test", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    assertThat(appender.getName()).isEqualTo("async-console-appender");
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.Test)

Example 10 with DropwizardLayoutFactory

use of io.dropwizard.logging.layout.DropwizardLayoutFactory in project dropwizard by dropwizard.

the class ConsoleAppenderFactoryTest method defaultIsNotNeverBlock.

@Test
public void defaultIsNotNeverBlock() throws Exception {
    ConsoleAppenderFactory<ILoggingEvent> consoleAppenderFactory = new ConsoleAppenderFactory<>();
    // default neverBlock
    AsyncAppender asyncAppender = (AsyncAppender) consoleAppenderFactory.build(new LoggerContext(), "test", new DropwizardLayoutFactory(), new NullLevelFilterFactory<>(), new AsyncLoggingEventAppenderFactory());
    assertThat(asyncAppender.isNeverBlock()).isFalse();
}
Also used : AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) AsyncAppender(ch.qos.logback.classic.AsyncAppender) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) DropwizardLayoutFactory(io.dropwizard.logging.layout.DropwizardLayoutFactory) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Aggregations

AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)19 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)19 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)17 Test (org.junit.Test)17 AsyncAppender (ch.qos.logback.classic.AsyncAppender)11 LoggerContext (ch.qos.logback.classic.LoggerContext)11 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)11 Logger (ch.qos.logback.classic.Logger)8 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)2 ThresholdLevelFilterFactory (io.dropwizard.logging.filter.ThresholdLevelFilterFactory)2 JMXConfigurator (ch.qos.logback.classic.jmx.JMXConfigurator)1 LevelChangePropagator (ch.qos.logback.classic.jul.LevelChangePropagator)1 FileSize (ch.qos.logback.core.util.FileSize)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 AsyncAppenderFactory (io.dropwizard.logging.async.AsyncAppenderFactory)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1