use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.
the class StringBuilderPool method createLog4j2Event.
private static LogEvent createLog4j2Event() {
final Marker marker = null;
final String fqcn = "com.mycom.myproject.mypackage.MyClass";
final Level level = Level.DEBUG;
// length=32
final String STR = "AB!(%087936DZYXQWEIOP$#^~-=/><nb";
final Message message = new SimpleMessage(STR);
final Throwable t = null;
final Map<String, String> 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).setContextMap(//
mdc).setContextStack(//
ndc).setThreadName(//
threadName).setSource(//
location).setTimeMillis(//
timestamp).build();
}
use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.
the class DefaultThreadContextStackTest method testModifyingImmutableOrNullThrowsException.
@Test
public void testModifyingImmutableOrNullThrowsException() {
final DefaultThreadContextStack stack = createStack();
final int originalSize = stack.size();
assertTrue(originalSize > 0);
final ContextStack actual = stack.getImmutableStackOrNull();
assertEquals(originalSize, actual.size());
assertThrows(UnsupportedOperationException.class, () -> actual.pop());
}
use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.
the class AsyncLogger method translateTo.
/*
* (non-Javadoc)
*
* @see com.lmax.disruptor.EventTranslatorVararg#translateTo(java.lang.Object, long, java.lang.Object[])
*/
@Override
public void translateTo(final RingBufferLogEvent event, final long sequence, final Object... args) {
// Implementation note: candidate for optimization: exceeds 35 bytecodes.
final AsyncLogger asyncLogger = (AsyncLogger) args[0];
final StackTraceElement location = (StackTraceElement) args[1];
final String fqcn = (String) args[2];
final Level level = (Level) args[3];
final Marker marker = (Marker) args[4];
final Message message = (Message) args[5];
final Throwable thrown = (Throwable) args[6];
// needs shallow copy to be fast (LOG4J2-154)
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Thread currentThread = Thread.currentThread();
final String threadName = THREAD_NAME_CACHING_STRATEGY.getThreadName();
event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, level, message, thrown, // in the AsyncLogger#actualAsyncLog method
CONTEXT_DATA_INJECTOR.injectContextData(null, (StringMap) event.getContextData()), contextStack, currentThread.getId(), threadName, currentThread.getPriority(), location, CLOCK, nanoClock);
}
use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.
@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
final StringMap contextData = ContextDataFactory.createContextData();
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");
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().setContextData(//
contextData).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(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());
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
assertEquals(event2, event, "copy constructor builder");
assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
}
use of org.apache.logging.log4j.ThreadContext.ContextStack in project logging-log4j2 by apache.
the class Log4jLogEventTest method testEquals.
@SuppressWarnings("deprecation")
@Test
public void testEquals() {
final StringMap contextData = ContextDataFactory.createContextData();
contextData.putValue("A", "B");
ThreadContext.push("first");
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().setContextData(//
contextData).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(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());
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent event2 = builder(event).build();
assertEquals(event2, event, "copy constructor builder");
assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
assertEquals(contextData, event2.getContextData());
assertSame(contextStack, event2.getContextStack());
assertTrue(event2.isEndOfBatch());
assertTrue(event2.isIncludeLocation());
assertSame(Level.FATAL, event2.getLevel());
assertSame(fqcn, event2.getLoggerFqcn());
assertSame(name, event2.getLoggerName());
assertSame(marker, event2.getMarker());
assertSame(message, event2.getMessage());
assertEquals(1234567890L, event2.getNanoTime());
assertSame(stackTraceElement, event2.getSource());
assertSame(threadName, event2.getThreadName());
assertSame(exception, event2.getThrown());
assertEquals(987654321L, event2.getTimeMillis());
final StringMap differentMap = ContextDataFactory.emptyFrozenContextData();
different("different contextMap", builder(event).setContextData(differentMap), event);
different("null contextMap", builder(event).setContextData(null), event);
ThreadContext.push("abc");
final ContextStack contextStack2 = ThreadContext.getImmutableStack();
different("different contextStack", builder(event).setContextStack(contextStack2), event);
different("null contextStack", builder(event).setContextStack(null), event);
different("different EndOfBatch", builder(event).setEndOfBatch(false), event);
different("different IncludeLocation", builder(event).setIncludeLocation(false), event);
different("different level", builder(event).setLevel(Level.INFO), event);
different("null level", builder(event).setLevel(null), event);
different("different fqcn", builder(event).setLoggerFqcn("different"), event);
different("null fqcn", builder(event).setLoggerFqcn(null), event);
different("different name", builder(event).setLoggerName("different"), event);
assertThrows(NullPointerException.class, () -> different("null name", builder(event).setLoggerName(null), event));
different("different marker", builder(event).setMarker(MarkerManager.getMarker("different")), event);
different("null marker", builder(event).setMarker(null), event);
different("different message", builder(event).setMessage(new ObjectMessage("different")), event);
assertThrows(NullPointerException.class, () -> different("null message", builder(event).setMessage(null), event));
different("different nanoTime", builder(event).setNanoTime(135), event);
different("different milliTime", builder(event).setTimeMillis(137), event);
final StackTraceElement stack2 = new StackTraceElement("XXX", "YYY", "file", 123);
different("different source", builder(event).setSource(stack2), event);
different("null source", builder(event).setSource(null), event);
different("different threadname", builder(event).setThreadName("different"), event);
different("null threadname", builder(event).setThreadName(null), event);
different("different exception", builder(event).setThrown(new Error("Boo!")), event);
different("null exception", builder(event).setThrown(null), event);
}
Aggregations