Search in sources :

Example 41 with LoggerContext

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();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Statement(java.sql.Statement) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 42 with LoggerContext

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"));
}
Also used : SmtpMessage(org.apache.logging.dumbster.smtp.SmtpMessage) SimpleSmtpServer(org.apache.logging.dumbster.smtp.SimpleSmtpServer) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 43 with LoggerContext

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());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 44 with LoggerContext

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());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 45 with LoggerContext

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();
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) ServletContext(javax.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4