Search in sources :

Example 26 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class SocketAppenderTest method testTcpAppenderDeadlock.

@Test
public void testTcpAppenderDeadlock() throws Exception {
    // @formatter:off
    final SocketAppender appender = SocketAppender.newBuilder().withHost("localhost").withPort(DYN_PORT).withReconnectDelayMillis(100).withName("test").withImmediateFail(false).build();
    // @formatter:on
    appender.start();
    // set appender on root and set level to debug
    logger.addAppender(appender);
    logger.setAdditive(false);
    logger.setLevel(Level.DEBUG);
    final TcpSocketTestServer tcpSocketServer = new TcpSocketTestServer(DYN_PORT);
    try {
        tcpSocketServer.start();
        logger.debug("This message is written because a deadlock never.");
        final LogEvent event = tcpSocketServer.getQueue().poll(3, TimeUnit.SECONDS);
        assertNotNull("No event retrieved", event);
    } finally {
        tcpSocketServer.shutdown();
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 27 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class SocketAppenderTest method testTcpAppender.

static void testTcpAppender(final TcpSocketTestServer tcpTestServer, final Logger logger, final int bufferSize) throws Exception {
    // @formatter:off
    final SocketAppender appender = SocketAppender.newBuilder().withHost("localhost").withPort(tcpTestServer.getLocalPort()).withReconnectDelayMillis(-1).withName("test").withImmediateFail(false).withBufferSize(bufferSize).build();
    // @formatter:on
    appender.start();
    Assert.assertEquals(bufferSize, appender.getManager().getByteBuffer().capacity());
    // set appender on root and set level to debug
    logger.addAppender(appender);
    logger.setAdditive(false);
    logger.setLevel(Level.DEBUG);
    final String tcKey = "UUID";
    final String expectedUuidStr = UUID.randomUUID().toString();
    ThreadContext.put(tcKey, expectedUuidStr);
    ThreadContext.push(expectedUuidStr);
    final String expectedExMsg = "This is a test";
    try {
        logger.debug("This is a test message");
        final Throwable child = new LoggingException(expectedExMsg);
        logger.error("Throwing an exception", child);
        logger.debug("This is another test message");
    } finally {
        ThreadContext.remove(tcKey);
        ThreadContext.pop();
    }
    Thread.sleep(250);
    LogEvent event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
    assertNotNull("No event retrieved", event);
    assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message"));
    assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 0);
    assertEquals(expectedUuidStr, event.getContextData().getValue(tcKey));
    event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS);
    assertNotNull("No event retrieved", event);
    assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("Throwing an exception"));
    assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 1);
    assertEquals(expectedUuidStr, event.getContextStack().pop());
    assertNotNull(event.getThrownProxy());
    assertEquals(expectedExMsg, event.getThrownProxy().getMessage());
}
Also used : LoggingException(org.apache.logging.log4j.LoggingException) LogEvent(org.apache.logging.log4j.core.LogEvent)

Example 28 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class MessagePatternConverterTest method testPattern.

@Test
public void testPattern() throws Exception {
    final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, null);
    Message msg = new SimpleMessage("Hello!");
    LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(msg).build();
    StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Unexpected result", "Hello!", sb.toString());
    event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(null).build();
    sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Incorrect length: " + sb, 0, sb.length());
    msg = new SimpleMessage(null);
    event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(msg).build();
    sb = new StringBuilder();
    converter.format(event, sb);
    assertEquals("Incorrect length: " + sb, 4, sb.length());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 29 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class NanoTimePatternConverterTest method testConverterAppendsLogEventNanoTimeToStringBuilder.

@Test
public void testConverterAppendsLogEventNanoTimeToStringBuilder() {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setNanoTime(1234567).build();
    final StringBuilder sb = new StringBuilder();
    final NanoTimePatternConverter converter = NanoTimePatternConverter.newInstance(null);
    converter.format(event, sb);
    assertEquals("1234567", sb.toString());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Example 30 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class NdcPatternConverterTest method testConverter.

private void testConverter(final String expected) {
    final Message msg = new SimpleMessage("Hello");
    final NdcPatternConverter converter = NdcPatternConverter.newInstance(null);
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "MyLogger").setLevel(//
    Level.DEBUG).setMessage(//
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    assertEquals(expected, str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)188 Test (org.junit.Test)150 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)127 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)94 Message (org.apache.logging.log4j.message.Message)33 Marker (org.apache.logging.log4j.Marker)16 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)11 HashMap (java.util.HashMap)9 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)8 File (java.io.File)7 IOException (java.io.IOException)7 Level (org.apache.logging.log4j.Level)7 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 Appender (org.apache.logging.log4j.core.Appender)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5