Search in sources :

Example 21 with JoranConfigurator

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

the class SamplingLoggerTest method createLoggerContext.

private LoggerContext createLoggerContext(String rootLevel, String appenderClassName) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element configuration = doc.createElement("configuration");
    doc.appendChild(configuration);
    Element appender = doc.createElement("appender");
    appender.setAttribute("name", "Test");
    appender.setAttribute("class", appenderClassName);
    configuration.appendChild(appender);
    Element rootLogger = doc.createElement("root");
    rootLogger.setAttribute("level", rootLevel);
    Element appenderRef = doc.createElement("appender-ref");
    appenderRef.setAttribute("ref", "Test");
    rootLogger.appendChild(appenderRef);
    configuration.appendChild(rootLogger);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    LoggerContext context = new LoggerContext();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    configurator.doConfigure(new InputSource(new StringReader(writer.toString())));
    return context;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) LoggerContext(ch.qos.logback.classic.LoggerContext)

Example 22 with JoranConfigurator

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

the class LoggersTest method testEffectiveLevel.

@Test
public void testEffectiveLevel() throws Exception {
    LoggerContext context = new LoggerContext();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    configurator.doConfigure(new InputSource(new StringReader(generateLogback("WARN", ImmutableMap.of("test", "INFO", "test.a", "ERROR", "test.a.X", "DEBUG", "test.a.X$1", "OFF")))));
    Assert.assertSame(context.getLogger("test"), Loggers.getEffectiveLogger(context, "test"));
    Assert.assertSame(context.getLogger("test.a"), Loggers.getEffectiveLogger(context, "test.a"));
    Assert.assertSame(context.getLogger("test.a.X"), Loggers.getEffectiveLogger(context, "test.a.X"));
    Assert.assertSame(context.getLogger("test.a.X$1"), Loggers.getEffectiveLogger(context, "test.a.X$1"));
    Assert.assertSame(context.getLogger(Logger.ROOT_LOGGER_NAME), Loggers.getEffectiveLogger(context, "defaultToRoot"));
    Assert.assertSame(context.getLogger("test"), Loggers.getEffectiveLogger(context, "test.defaultToTest"));
    Assert.assertSame(context.getLogger("test.a"), Loggers.getEffectiveLogger(context, "test.a.defaultToTestDotA"));
    Assert.assertSame(context.getLogger("test.a.X"), Loggers.getEffectiveLogger(context, "test.a.X.defaultToTestDotADotX"));
}
Also used : InputSource(org.xml.sax.InputSource) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) StringReader(java.io.StringReader) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 23 with JoranConfigurator

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

the class RollingLocationLogAppenderTest method testRollOver.

@Test
public void testRollOver() 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, 20000);
    Map<LocationIdentifier, LocationOutputStream> activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(1, activeFiles.size());
    LocationOutputStream locationOutputStream = activeFiles.get(new LocationIdentifier("testNs", "testApp"));
    Location parentDir = Locations.getParent(locationOutputStream.getLocation());
    Assert.assertEquals(10, parentDir.list().size());
    // different program should go to different directory
    addTagsToMdc("testNs", "testApp1");
    ingestLogs(logger, 20000);
    activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(2, activeFiles.size());
    locationOutputStream = activeFiles.get(new LocationIdentifier("testNs", "testApp1"));
    parentDir = Locations.getParent(locationOutputStream.getLocation());
    Assert.assertEquals(10, parentDir.list().size());
    // different program should go to different directory because namespace is different
    addTagsToMdc("testNs1", "testApp1");
    ingestLogs(logger, 20000);
    activeFiles = rollingAppender.getLocationManager().getActiveLocations();
    Assert.assertEquals(3, activeFiles.size());
    locationOutputStream = activeFiles.get(new LocationIdentifier("testNs1", "testApp1"));
    parentDir = Locations.getParent(locationOutputStream.getLocation());
    Assert.assertEquals(10, parentDir.list().size());
}
Also used : NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) Logger(org.slf4j.Logger) LocationFactory(org.apache.twill.filesystem.LocationFactory) 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) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 24 with JoranConfigurator

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

the class KafkaLogProcessorPipelineTest method createLoggerContext.

private LoggerContext createLoggerContext(String rootLevel, Map<String, String> loggerLevels, String appenderClassName) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element configuration = doc.createElement("configuration");
    doc.appendChild(configuration);
    Element appender = doc.createElement("appender");
    appender.setAttribute("name", "Test");
    appender.setAttribute("class", appenderClassName);
    configuration.appendChild(appender);
    for (Map.Entry<String, String> entry : loggerLevels.entrySet()) {
        Element logger = doc.createElement("logger");
        logger.setAttribute("name", entry.getKey());
        logger.setAttribute("level", entry.getValue());
        configuration.appendChild(logger);
    }
    Element rootLogger = doc.createElement("root");
    rootLogger.setAttribute("level", rootLevel);
    Element appenderRef = doc.createElement("appender-ref");
    appenderRef.setAttribute("ref", "Test");
    rootLogger.appendChild(appenderRef);
    configuration.appendChild(rootLogger);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    LoggerContext context = new LoggerContext();
    JoranConfigurator configurator = new LogPipelineConfigurator(CConfiguration.create());
    configurator.setContext(context);
    configurator.doConfigure(new InputSource(new StringReader(writer.toString())));
    return context;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) LoggerContext(ch.qos.logback.classic.LoggerContext) StringWriter(java.io.StringWriter) JoranConfigurator(ch.qos.logback.classic.joran.JoranConfigurator) StringReader(java.io.StringReader) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LogPipelineConfigurator(co.cask.cdap.logging.pipeline.LogPipelineConfigurator)

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