Search in sources :

Example 76 with LogEvent

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

the class InMemoryAppenderTest method assertMessage.

private void assertMessage(final String string, final InMemoryAppender app, final String header) {
    final LogEvent event = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "TestLogger").setLoggerFqcn(//
    InMemoryAppenderTest.class.getName()).setLevel(//
    Level.INFO).setMessage(//
    new SimpleMessage("Test")).build();
    app.start();
    assertTrue("Appender did not start", app.isStarted());
    app.append(event);
    app.append(event);
    final String msg = app.toString();
    assertNotNull("No message", msg);
    final String expectedHeader = header == null ? "" : header;
    final String expected = expectedHeader + "Test" + Strings.LINE_SEPARATOR + "Test" + Strings.LINE_SEPARATOR;
    assertTrue("Incorrect message: " + msg, msg.equals(expected));
    app.stop();
    assertFalse("Appender did not stop", app.isStarted());
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 77 with LogEvent

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

the class Log4jLogEventTest method testTimestampGeneratedByClock.

@Test
public void testTimestampGeneratedByClock() {
    final LogEvent evt = Log4jLogEvent.newBuilder().build();
    assertEquals(FixedTimeClock.FIXED_TIME, evt.getTimeMillis());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 78 with LogEvent

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

the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.

@SuppressWarnings("deprecation")
@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
    final Map<String, String> contextMap = new HashMap<>();
    contextMap.put("A", "B");
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Exception exception = new Exception("test");
    final Marker marker = MarkerManager.getMarker("EVENTTEST");
    final Message message = new SimpleMessage("foo");
    final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final Log4jLogEvent event = //
    Log4jLogEvent.newBuilder().setContextMap(//
    contextMap).setContextStack(//
    contextStack).setEndOfBatch(//
    true).setIncludeLocation(//
    true).setLevel(//
    Level.FATAL).setLoggerFqcn(//
    fqcn).setLoggerName(//
    name).setMarker(//
    marker).setMessage(//
    message).setNanoTime(//
    1234567890L).setSource(//
    stackTraceElement).setThreadName(//
    threadName).setThrown(//
    exception).setTimeMillis(987654321L).build();
    assertEquals(contextMap, event.getContextMap());
    assertSame(contextStack, event.getContextStack());
    assertEquals(true, event.isEndOfBatch());
    assertEquals(true, event.isIncludeLocation());
    assertSame(Level.FATAL, event.getLevel());
    assertSame(fqcn, event.getLoggerFqcn());
    assertSame(name, event.getLoggerName());
    assertSame(marker, event.getMarker());
    assertSame(message, event.getMessage());
    assertEquals(1234567890L, event.getNanoTime());
    assertSame(stackTraceElement, event.getSource());
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
    assertEquals("copy constructor builder", event2, event);
    assertEquals("same hashCode", event2.hashCode(), event.hashCode());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 79 with LogEvent

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

the class Log4jLogEventTest method testToImmutableSame.

@Test
public void testToImmutableSame() {
    final LogEvent logEvent = new Log4jLogEvent();
    Assert.assertSame(logEvent, logEvent.toImmutable());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 80 with LogEvent

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

the class MutableLogEventTest method testToImmutable.

@Test
public void testToImmutable() {
    final LogEvent logEvent = new MutableLogEvent();
    Assert.assertNotSame(logEvent, logEvent.toImmutable());
}
Also used : RingBufferLogEvent(org.apache.logging.log4j.core.async.RingBufferLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.Test)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)189 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 Appender (org.apache.logging.log4j.core.Appender)6 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)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