use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class AbstractJpaAppenderTest method tearDown.
public void tearDown() throws SQLException {
final LoggerContext context = LoggerContext.getContext(false);
try {
final Appender appender = context.getConfiguration().getAppender("databaseAppender");
assertNotNull("The appender should not be null.", appender);
assertTrue("The appender should be a JpaAppender.", appender instanceof JpaAppender);
((JpaAppender) appender).getManager().close();
} finally {
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
context.reconfigure();
StatusLogger.getLogger().reset();
try (Statement statement = this.connection.createStatement()) {
statement.execute("SHUTDOWN");
}
this.connection.close();
}
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class SmtpAppenderTest method testDelivery.
@Test
public void testDelivery() {
final String subjectKey = getClass().getName();
final String subjectValue = "SubjectValue1";
ThreadContext.put(subjectKey, subjectValue);
final SmtpAppender appender = SmtpAppender.createAppender(null, "Test", "to@example.com", "cc@example.com", "bcc@example.com", "from@example.com", "replyTo@example.com", "Subject Pattern %X{" + subjectKey + "}", null, HOST, PORT, null, null, "false", "3", null, null, "true");
appender.start();
final LoggerContext context = LoggerContext.getContext();
final Logger root = context.getLogger("SMTPAppenderTest");
root.addAppender(appender);
root.setAdditive(false);
root.setLevel(Level.DEBUG);
final SimpleSmtpServer server = SimpleSmtpServer.start(PORTNUM);
root.debug("Debug message #1");
root.debug("Debug message #2");
root.debug("Debug message #3");
root.debug("Debug message #4");
root.error("Error with exception", new RuntimeException("Exception message"));
root.error("Error message #2");
server.stop();
assertTrue(server.getReceivedEmailSize() == 2);
final Iterator<SmtpMessage> messages = server.getReceivedEmail();
final SmtpMessage email = messages.next();
assertEquals("to@example.com", email.getHeaderValue("To"));
assertEquals("cc@example.com", email.getHeaderValue("Cc"));
// assertEquals("bcc@example.com", email.getHeaderValue("Bcc")); // BCC
// can't be tested with Dumpster 1.6
assertEquals("from@example.com", email.getHeaderValue("From"));
assertEquals("replyTo@example.com", email.getHeaderValue("Reply-To"));
final String headerValue = email.getHeaderValue("Subject");
assertEquals(headerValue, "Subject Pattern " + subjectValue);
final String body = email.getBody();
assertFalse(body.contains("Debug message #1"));
assertTrue(body.contains("Debug message #2"));
assertTrue(body.contains("Debug message #3"));
assertTrue(body.contains("Debug message #4"));
assertTrue(body.contains("Error with exception"));
assertTrue(body.contains("RuntimeException"));
assertTrue(body.contains("Exception message"));
assertFalse(body.contains("Error message #2"));
final SmtpMessage email2 = messages.next();
final String body2 = email2.getBody();
assertFalse(body2.contains("Debug message #4"));
assertFalse(body2.contains("Error with exception"));
assertTrue(body2.contains("Error message #2"));
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class RegexReplacementConverterTest method testReplacement.
@Test
public void testReplacement() {
ThreadContext.put("MyKey", "Apache");
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
RegexReplacementConverterTest.class.getName()).setLevel(//
Level.DEBUG).setMessage(//
new SimpleMessage("This is a test")).build();
final StringBuilder sb = new StringBuilder();
final LoggerContext ctx = LoggerContext.getContext();
final String[] options = new String[] { "%logger %msg%n", "\\.", "/" };
final RegexReplacementConverter converter = RegexReplacementConverter.newInstance(ctx.getConfiguration(), options);
converter.format(event, sb);
assertEquals("org/apache/logging/log4j/core/pattern/RegexReplacementConverterTest This is a test" + Strings.LINE_SEPARATOR, sb.toString());
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class VariablesNotEmptyReplacementConverterTest method testReplacement.
private void testReplacement(final String tag, final String expectedValue) {
final LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
VariablesNotEmptyReplacementConverterTest.class.getName()).setLevel(//
Level.DEBUG).setMessage(//
new SimpleMessage("This is a test")).build();
final StringBuilder sb = new StringBuilder();
final LoggerContext ctx = LoggerContext.getContext();
final String[] options = new String[] { "[" + tag + "]" };
final VariablesNotEmptyReplacementConverter converter = VariablesNotEmptyReplacementConverter.newInstance(ctx.getConfiguration(), options);
converter.format(event, sb);
assertEquals(expectedValue, sb.toString());
}
use of org.apache.logging.log4j.core.LoggerContext in project logging-log4j2 by apache.
the class WebLookupTest method testLookup2.
@Test
public void testLookup2() throws Exception {
ContextAnchor.THREAD_CONTEXT.remove();
final ServletContext servletContext = new MockServletContext();
servletContext.setAttribute("TestAttr", "AttrValue");
servletContext.setInitParameter("myapp.logdir", "target");
servletContext.setAttribute("Name1", "Ben");
servletContext.setInitParameter("Name2", "Jerry");
servletContext.setInitParameter("log4jConfiguration", "WEB-INF/classes/log4j-webvar.xml");
final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
initializer.start();
initializer.setLoggerContext();
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
assertNotNull("No LoggerContext", ctx);
assertNotNull("No ServletContext", ctx.getExternalContext());
final Configuration config = ctx.getConfiguration();
assertNotNull("No Configuration", config);
final Map<String, Appender> appenders = config.getAppenders();
for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
if (entry.getKey().equals("file")) {
final FileAppender fa = (FileAppender) entry.getValue();
assertEquals("target/myapp.log", fa.getFileName());
}
}
initializer.stop();
ContextAnchor.THREAD_CONTEXT.remove();
}
Aggregations