Search in sources :

Example 11 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project cdap by caskdata.

the class RollingLocationLogAppenderTest method testRollingLocationLogAppender.

@Test
public void testRollingLocationLogAppender() throws Exception {
    // assume SLF4J is bound to logback in the current environment
    AppenderContext appenderContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(appenderContext);
    // Call context.reset() to clear any previous configuration, e.g. default
    // configuration. For multi-step configuration, omit calling context.reset().
    appenderContext.reset();
    configurator.doConfigure(getClass().getResourceAsStream("/rolling-appender-logback-test.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(appenderContext);
    RollingLocationLogAppender rollingAppender = (RollingLocationLogAppender) appenderContext.getLogger(RollingLocationLogAppenderTest.class).getAppender("rollingAppender");
    addTagsToMdc("testNamespace", "testApp");
    Logger logger = appenderContext.getLogger(RollingLocationLogAppenderTest.class);
    ingestLogs(logger, 5);
    Map<LocationIdentifier, LocationOutputStream> activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(1, activeFiles.size());
    verifyFileOutput(activeFiles, 5);
    // different program should go to different directory
    addTagsToMdc("testNamespace", "testApp1");
    ingestLogs(logger, 5);
    activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(2, activeFiles.size());
    verifyFileOutput(activeFiles, 5);
    // different program should go to different directory because namespace is different
    addTagsToMdc("testNamespace1", "testApp1");
    ingestLogs(logger, 5);
    activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(3, activeFiles.size());
    verifyFileOutput(activeFiles, 5);
}
Also used : DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) AppenderContext(co.cask.cdap.api.logging.AppenderContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) Logger(org.slf4j.Logger) LocationFactory(org.apache.twill.filesystem.LocationFactory) Test(org.junit.Test)

Example 12 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project cdap by caskdata.

the class RollingLocationLogAppenderTest method testFileClose.

@Test
public void testFileClose() throws Exception {
    // assume SLF4J is bound to logback in the current environment
    AppenderContext appenderContext = new LocalAppenderContext(injector.getInstance(DatasetFramework.class), injector.getInstance(TransactionSystemClient.class), injector.getInstance(LocationFactory.class), new NoOpMetricsCollectionService());
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(appenderContext);
    // Call context.reset() to clear any previous configuration, e.g. default
    // configuration. For multi-step configuration, omit calling context.reset().
    appenderContext.reset();
    configurator.doConfigure(getClass().getResourceAsStream("/rolling-appender-logback-test.xml"));
    StatusPrinter.printInCaseOfErrorsOrWarnings(appenderContext);
    RollingLocationLogAppender rollingAppender = (RollingLocationLogAppender) appenderContext.getLogger(RollingLocationLogAppenderTest.class).getAppender("rollingAppender");
    addTagsToMdc("testNs", "testApp");
    Logger logger = appenderContext.getLogger(RollingLocationLogAppenderTest.class);
    ingestLogs(logger, 20);
    // wait for 500 ms so that file is eligible for closing
    Thread.sleep(500);
    // flush to make sure file is closed
    rollingAppender.flush();
    Assert.assertEquals(0, rollingAppender.getLocationManager().getActiveLocations().size());
}
Also used : DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LocalAppenderContext(co.cask.cdap.logging.framework.LocalAppenderContext) AppenderContext(co.cask.cdap.api.logging.AppenderContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) Logger(org.slf4j.Logger) LocationFactory(org.apache.twill.filesystem.LocationFactory) Test(org.junit.Test)

Example 13 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator 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 14 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project stashbot by palantir.

the class PluginLoggerFactory method init.

private void init() {
    // Assumes LSF4J is bound to logback
    context = (LoggerContext) LoggerFactory.getILoggerFactory();
    // store the home dir to use for relative paths
    context.putProperty("stash.home", homeDir);
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    InputStream is;
    is = this.getClass().getClassLoader().getResourceAsStream("logback-test.xml");
    if (is != null) {
        stashRootLogger.info("Using logback-test.xml for logger settings");
    } else {
        stashRootLogger.info("Using logback.xml for logger settings");
        is = this.getClass().getClassLoader().getResourceAsStream("logback.xml");
    }
    try {
        configurator.doConfigure(is);
    } catch (JoranException e) {
        System.err.println("Error configuring logging framework" + e);
    }
}
Also used : JoranException(ch.qos.logback.core.joran.spi.JoranException) InputStream(java.io.InputStream) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator)

Example 15 with JoranConfigurator

use of ch.qos.logback.classic.joran.JoranConfigurator in project sonarqube by SonarSource.

the class Logback method configure.

/**
   * Note that this method closes the input stream
   */
private static void configure(InputStream input, Map<String, String> substitutionVariables) {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(configureContext(lc, substitutionVariables));
        configurator.doConfigure(input);
    } catch (JoranException e) {
    // StatusPrinter will handle this
    } finally {
        IOUtils.closeQuietly(input);
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
}
Also used : JoranException(ch.qos.logback.core.joran.spi.JoranException) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

JoranConfigurator (ch.qos.logback.classic.joran.JoranConfigurator)24 LoggerContext (ch.qos.logback.classic.LoggerContext)15 JoranException (ch.qos.logback.core.joran.spi.JoranException)12 File (java.io.File)7 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 Logger (org.slf4j.Logger)4 AppenderContext (co.cask.cdap.api.logging.AppenderContext)3 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)3 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)3 LocalAppenderContext (co.cask.cdap.logging.framework.LocalAppenderContext)3 StringReader (java.io.StringReader)3 URL (java.net.URL)3 InputSource (org.xml.sax.InputSource)3 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 ContextInitializer (ch.qos.logback.classic.util.ContextInitializer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringWriter (java.io.StringWriter)2 Transformer (javax.xml.transform.Transformer)2