Search in sources :

Example 51 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project coffeenet-starter by coffeenet.

the class CoffeeNetLoggingAutoConfigurationTest method integration.

@Test
public void integration() {
    final Logger logger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
    assertThat(logger.getAppender("CONSOLE"), is(instanceOf(ConsoleAppender.class)));
    Appender<ILoggingEvent> fileAppender = logger.getAppender("COFFEENET-FILE");
    assertThat(fileAppender, is(instanceOf(RollingFileAppender.class)));
    // Check File Appender
    RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;
    assertThat(rollingFileAppender.getFile(), is("logs/app.log"));
    assertThat(rollingFileAppender.getName(), is("COFFEENET-FILE"));
    RollingPolicy rollingPolicy = rollingFileAppender.getRollingPolicy();
    assertThat(rollingPolicy, is(instanceOf(TimeBasedRollingPolicy.class)));
    TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) rollingFileAppender.getRollingPolicy();
    assertThat(timeBasedRollingPolicy.getFileNamePattern(), is("logs/app-%d{yyyy-MM-dd}.log"));
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) rollingFileAppender.getEncoder();
    assertThat(encoder.getPattern(), is("%d{yyyy-MM-dd HH:mm:ss.SSS} %5p --- [%t] %-40.40logger{39} : %m%n%wEx"));
    // Check Gelf Appender
    Appender<ILoggingEvent> gelfAppender = logger.getAppender("COFFEENET-GELF");
    assertThat(gelfAppender, is(instanceOf(GelfAppender.class)));
    GelfAppender gelfAppender1 = (GelfAppender) gelfAppender;
    assertThat(gelfAppender1.getName(), is("COFFEENET-GELF"));
    assertThat(gelfAppender1.getServer(), is("localServer"));
    assertThat(gelfAppender1.getProtocol(), is("TCP"));
    assertThat(gelfAppender1.getPort(), is(1337));
    assertThat(((PatternLayout) gelfAppender1.getLayout()).getPattern(), is("%m"));
    assertThat(gelfAppender1.getAdditionalFields(), hasEntry("environment", "test"));
    assertThat(gelfAppender1.getAdditionalFields(), hasEntry("application", "CoffeeNetApplication"));
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) GelfAppender(de.appelgriepsch.logback.GelfAppender) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 52 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project dropwizard by dropwizard.

the class BootstrapLogging method bootstrap.

public static void bootstrap(Level level) {
    LoggingUtil.hijackJDKLogging();
    BOOTSTRAPPING_LOCK.lock();
    try {
        if (bootstrapped) {
            return;
        }
        final Logger root = LoggingUtil.getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
        root.detachAndStopAllAppenders();
        final DropwizardLayout formatter = new DropwizardLayout(root.getLoggerContext(), TimeZone.getDefault());
        formatter.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(formatter);
        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 53 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent 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 54 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent 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 55 with ILoggingEvent

use of ch.qos.logback.classic.spi.ILoggingEvent in project dropwizard by dropwizard.

the class LogbackClassicRequestLogFactory method build.

public RequestLog build(String name) {
    final Logger logger = (Logger) LoggerFactory.getLogger("http.request");
    logger.setAdditive(false);
    final LoggerContext context = logger.getLoggerContext();
    final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new NullLevelFilterFactory<>();
    final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
    final LayoutFactory<ILoggingEvent> layoutFactory = (c, tz) -> new RequestLogLayout();
    final AppenderAttachableImpl<ILoggingEvent> attachable = new AppenderAttachableImpl<>();
    for (AppenderFactory<ILoggingEvent> appender : appenders) {
        attachable.addAppender(appender.build(context, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
    }
    return new DropwizardSlf4jRequestLog(attachable, timeZone);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ConsoleAppenderFactory(io.dropwizard.logging.ConsoleAppenderFactory) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerFactory(org.slf4j.LoggerFactory) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory) LoggerContext(ch.qos.logback.classic.LoggerContext) Valid(javax.validation.Valid) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) RequestLogFactory(io.dropwizard.request.logging.RequestLogFactory) ImmutableList(com.google.common.collect.ImmutableList) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) CoreConstants(ch.qos.logback.core.CoreConstants) PatternLayoutBase(ch.qos.logback.core.pattern.PatternLayoutBase) AppenderAttachableImpl(ch.qos.logback.core.spi.AppenderAttachableImpl) RequestLog(org.eclipse.jetty.server.RequestLog) ImmutableMap(com.google.common.collect.ImmutableMap) LayoutFactory(io.dropwizard.logging.layout.LayoutFactory) TimeZone(java.util.TimeZone) AppenderFactory(io.dropwizard.logging.AppenderFactory) LevelFilterFactory(io.dropwizard.logging.filter.LevelFilterFactory) NotNull(javax.validation.constraints.NotNull) Logger(ch.qos.logback.classic.Logger) AsyncAppenderFactory(io.dropwizard.logging.async.AsyncAppenderFactory) NullLevelFilterFactory(io.dropwizard.logging.filter.NullLevelFilterFactory) AppenderAttachableImpl(ch.qos.logback.core.spi.AppenderAttachableImpl) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) AsyncLoggingEventAppenderFactory(io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)106 Test (org.junit.Test)47 LoggerContext (ch.qos.logback.classic.LoggerContext)37 Logger (ch.qos.logback.classic.Logger)34 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)19 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)18 FileAppender (ch.qos.logback.core.FileAppender)17 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)17 File (java.io.File)15 Appender (ch.qos.logback.core.Appender)12 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)12 AsyncAppender (ch.qos.logback.classic.AsyncAppender)10 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)10 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)10 ArrayList (java.util.ArrayList)9 Logger (org.slf4j.Logger)9 LogMessage (co.cask.cdap.logging.appender.LogMessage)7 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)6 IOException (java.io.IOException)6 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)6