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");
}
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());
}
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);
}
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();
}
Aggregations