Search in sources :

Example 96 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project cdap-ingest by caskdata.

the class TailerLogUtils method getSizeLogger.

public static Logger getSizeLogger(String file, String fileSize) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setFileNamePattern(file + "%i");
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.start();
    rollingPolicy.setMaxIndex(100);
    fileAppender.setRollingPolicy(rollingPolicy);
    SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setContext(loggerContext);
    triggeringPolicy.setMaxFileSize(fileSize);
    triggeringPolicy.start();
    fileAppender.setTriggeringPolicy(triggeringPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("[%d  %-5p %c{1}] %msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    // configures  logger
    Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "size");
    rootLogger.setLevel(Level.DEBUG);
    rootLogger.addAppender(fileAppender);
    return rootLogger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 97 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project camel by apache.

the class ITestApplication method overrideLoggingConfig.

private static void overrideLoggingConfig() {
    URL logbackFile = ITestApplication.class.getResource("/spring-logback.xml");
    if (logbackFile != null) {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            // Call context.reset() to clear any previous configuration, e.g. default
            // configuration. For multi-step configuration, omit calling context.reset().
            context.reset();
            configurator.doConfigure(logbackFile);
        } catch (JoranException je) {
        // StatusPrinter will handle this
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(context);
    }
}
Also used : JoranException(ch.qos.logback.core.joran.spi.JoranException) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LoggerContext(ch.qos.logback.classic.LoggerContext) URL(java.net.URL)

Example 98 with LoggerContext

use of ch.qos.logback.classic.LoggerContext in project weave by continuuity.

the class ServiceMain method doMain.

protected final void doMain(final ZKClientService zkClientService, final Service service) throws ExecutionException, InterruptedException {
    configureLogger();
    final String serviceName = service.toString();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            Services.chainStop(service, zkClientService);
        }
    });
    // Listener for state changes of the service
    ListenableFuture<Service.State> completion = Services.getCompletionFuture(service);
    // Starts the service
    LOG.info("Starting service {}.", serviceName);
    Futures.getUnchecked(Services.chainStart(zkClientService, service));
    LOG.info("Service {} started.", serviceName);
    try {
        completion.get();
        LOG.info("Service {} completed.", serviceName);
    } catch (Throwable t) {
        LOG.warn("Exception thrown from service {}.", serviceName, t);
        throw Throwables.propagate(t);
    } finally {
        ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
        if (loggerFactory instanceof LoggerContext) {
            ((LoggerContext) loggerFactory).stop();
        }
    }
}
Also used : ILoggerFactory(org.slf4j.ILoggerFactory) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 99 with LoggerContext

use of ch.qos.logback.classic.LoggerContext 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)

Example 100 with LoggerContext

use of ch.qos.logback.classic.LoggerContext 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

LoggerContext (ch.qos.logback.classic.LoggerContext)200 Test (org.junit.Test)133 Logger (ch.qos.logback.classic.Logger)39 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)39 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)21 FileAppender (ch.qos.logback.core.FileAppender)19 File (java.io.File)18 JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)15 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)15 Appender (ch.qos.logback.core.Appender)14 NullLevelFilterFactory (io.dropwizard.logging.filter.NullLevelFilterFactory)13 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)12 AsyncLoggingEventAppenderFactory (io.dropwizard.logging.async.AsyncLoggingEventAppenderFactory)12 AsyncAppender (ch.qos.logback.classic.AsyncAppender)11 DropwizardLayoutFactory (io.dropwizard.logging.layout.DropwizardLayoutFactory)11 JoranException (ch.qos.logback.core.joran.spi.JoranException)10 IOException (java.io.IOException)7 Logger (org.slf4j.Logger)7 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)6 ArrayList (java.util.ArrayList)6