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;
}
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"));
}
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());
}
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;
}
Aggregations