Search in sources :

Example 16 with ContextStack

use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.

the class Log4jLogEventTest method testBuilderCorrectlyCopiesMutableLogEvent.

@Test
public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception {
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("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");
    new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final MutableLogEvent event = new MutableLogEvent();
    event.setContextData(contextData);
    event.setContextStack(contextStack);
    event.setEndOfBatch(true);
    event.setIncludeLocation(true);
    // event.setSource(stackTraceElement); // cannot be explicitly set
    event.setLevel(Level.FATAL);
    event.setLoggerFqcn(fqcn);
    event.setLoggerName(name);
    event.setMarker(marker);
    event.setMessage(message);
    event.setNanoTime(1234567890L);
    event.setThreadName(threadName);
    event.setThrown(exception);
    event.setTimeMillis(987654321L);
    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertTrue(event.isEndOfBatch());
    assertTrue(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()); // don't invoke
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent e2 = new Log4jLogEvent.Builder(event).build();
    assertEquals(contextData, e2.getContextData());
    assertSame(contextStack, e2.getContextStack());
    assertTrue(e2.isEndOfBatch());
    assertTrue(e2.isIncludeLocation());
    assertSame(Level.FATAL, e2.getLevel());
    assertSame(fqcn, e2.getLoggerFqcn());
    assertSame(name, e2.getLoggerName());
    assertSame(marker, e2.getMarker());
    assertSame(message, e2.getMessage());
    assertEquals(1234567890L, e2.getNanoTime());
    // assertSame(stackTraceElement, e2.getSource()); // don't invoke
    assertSame(threadName, e2.getThreadName());
    assertSame(exception, e2.getThrown());
    assertEquals(987654321L, e2.getTimeMillis());
    // use reflection to get value of source field in log event copy:
    // invoking the getSource() method would initialize the field
    final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source");
    fieldSource.setAccessible(true);
    final Object value = fieldSource.get(e2);
    assertNull(value, "source in copy");
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) 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) 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) Field(java.lang.reflect.Field) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Example 17 with ContextStack

use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.

the class RingBufferLogEventTest method testGetMillisReturnsConstructorMillisForNormalMessage.

@Test
public void testGetMillisReturnsConstructorMillisForNormalMessage() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = null;
    final Marker marker = null;
    final String fqcn = null;
    final Level level = null;
    final Message data = null;
    final Throwable t = null;
    final ContextStack contextStack = null;
    final String threadName = null;
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(123, 456), new DummyNanoClock(1));
    assertEquals(123, evt.getTimeMillis());
    assertEquals(456, evt.getInstant().getNanoOfMillisecond());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.Test)

Example 18 with ContextStack

use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.

the class DefaultThreadContextStackTest method testImmutableOrNullReturnsCopyOfContents.

@Test
public void testImmutableOrNullReturnsCopyOfContents() {
    final DefaultThreadContextStack stack = createStack();
    assertFalse(stack.isEmpty());
    final ContextStack actual = stack.getImmutableStackOrNull();
    assertNotNull(actual);
    assertEquals(stack, actual);
}
Also used : ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.jupiter.api.Test)

Example 19 with ContextStack

use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.

the class PatternLayoutComparisonBenchmark method createLog4j2Event.

private static LogEvent createLog4j2Event() {
    final Marker marker = null;
    final String fqcn = "com.mycom.myproject.mypackage.MyClass";
    final Level level = Level.DEBUG;
    final Message message = new SimpleMessage(STR);
    final Throwable t = null;
    final StringMap mdc = null;
    final ContextStack ndc = null;
    final String threadName = null;
    final StackTraceElement location = null;
    final long timestamp = 12345678;
    return // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "name(ignored)").setMarker(// 
    marker).setLoggerFqcn(// 
    fqcn).setLevel(// 
    level).setMessage(// 
    message).setThrown(// 
    t).setContextData(// 
    mdc).setContextStack(// 
    ndc).setThreadName(// 
    threadName).setSource(// 
    location).setTimeMillis(// 
    timestamp).build();
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack)

Aggregations

ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)19 Marker (org.apache.logging.log4j.Marker)17 Message (org.apache.logging.log4j.message.Message)17 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)16 Level (org.apache.logging.log4j.Level)13 StringMap (org.apache.logging.log4j.util.StringMap)12 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)8 FixedPreciseClock (org.apache.logging.log4j.core.time.internal.FixedPreciseClock)8 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)8 Test (org.junit.Test)8 Test (org.junit.jupiter.api.Test)6 LogEvent (org.apache.logging.log4j.core.LogEvent)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 IOException (java.io.IOException)4 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)4 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)4 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)4 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)4 ReusableMessageFactory (org.apache.logging.log4j.message.ReusableMessageFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1