Search in sources :

Example 1 with ContextStack

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();
}
Also used : 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)

Example 2 with ContextStack

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());
}
Also used : ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.jupiter.api.Test)

Example 3 with ContextStack

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);
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack)

Example 4 with ContextStack

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");
}
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) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Example 5 with ContextStack

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);
}
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) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

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