Search in sources :

Example 16 with SortedArrayStringMap

use of org.apache.logging.log4j.util.SortedArrayStringMap 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 SortedArrayStringMap

use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.

the class ContextDataJsonAttributeConverterTest method testConvert01.

@Test
public void testConvert01() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("test1", "another1");
    map.putValue("key2", "value2");
    final String converted = this.converter.convertToDatabaseColumn(map);
    assertNotNull("The converted value should not be null.", converted);
    final ReadOnlyStringMap reversed = this.converter.convertToEntityAttribute(converted);
    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", map, reversed);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) ReadOnlyStringMap(org.apache.logging.log4j.util.ReadOnlyStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 18 with SortedArrayStringMap

use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.

the class ContextDataAttributeConverterTest method testConvertToDatabaseColumn01.

@Test
public void testConvertToDatabaseColumn01() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("test1", "another1");
    map.putValue("key2", "value2");
    assertEquals("The converted value is not correct.", map.toString(), this.converter.convertToDatabaseColumn(map));
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 19 with SortedArrayStringMap

use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.

the class ContextDataAttributeConverterTest method testConvertToDatabaseColumn02.

@Test
public void testConvertToDatabaseColumn02() {
    final StringMap map = new SortedArrayStringMap();
    map.putValue("someKey", "coolValue");
    map.putValue("anotherKey", "testValue");
    map.putValue("myKey", "yourValue");
    assertEquals("The converted value is not correct.", map.toString(), this.converter.convertToDatabaseColumn(map));
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test)

Example 20 with SortedArrayStringMap

use of org.apache.logging.log4j.util.SortedArrayStringMap in project logging-log4j2 by apache.

the class JsonLayoutTest method testLayoutRingBufferEventReusableMessageWithCurlyBraces.

// Test for LOG4J2-2312 LOG4J2-2341
@Test
public void testLayoutRingBufferEventReusableMessageWithCurlyBraces() throws Exception {
    final boolean propertiesAsList = false;
    final AbstractJacksonLayout layout = JsonLayout.newBuilder().setLocationInfo(false).setProperties(false).setPropertiesAsList(propertiesAsList).setComplete(false).setCompact(true).setEventEol(false).setCharset(StandardCharsets.UTF_8).setIncludeStacktrace(true).build();
    Message message = ReusableMessageFactory.INSTANCE.newMessage("Testing {}", new TestObj());
    try {
        RingBufferLogEvent ringBufferEvent = new RingBufferLogEvent();
        ringBufferEvent.setValues(null, "a.B", null, "f.q.c.n", Level.DEBUG, message, null, new SortedArrayStringMap(), ThreadContext.EMPTY_STACK, 1L, "threadName", 1, null, new SystemClock(), new DummyNanoClock());
        final String str = layout.toSerializable(ringBufferEvent);
        final String expectedMessage = "Testing " + TestObj.TO_STRING_VALUE;
        assertThat(str, containsString("\"message\":\"" + expectedMessage + '"'));
        final Log4jLogEvent actual = new Log4jJsonObjectMapper(propertiesAsList, true, false, false).readValue(str, Log4jLogEvent.class);
        assertEquals(expectedMessage, actual.getMessage().getFormattedMessage());
    } finally {
        ReusableMessageFactory.release(message);
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) SystemClock(org.apache.logging.log4j.core.time.internal.SystemClock) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Log4jJsonObjectMapper(org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) RingBufferLogEvent(org.apache.logging.log4j.core.async.RingBufferLogEvent) Test(org.junit.Test)

Aggregations

SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)28 StringMap (org.apache.logging.log4j.util.StringMap)19 LogEvent (org.apache.logging.log4j.core.LogEvent)9 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)9 Test (org.junit.Test)9 Test (org.junit.jupiter.api.Test)9 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)8 Message (org.apache.logging.log4j.message.Message)4 ReadOnlyStringMap (org.apache.logging.log4j.util.ReadOnlyStringMap)4 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Random (java.util.Random)2 LogEventAdapter (org.apache.log4j.bridge.LogEventAdapter)2 LocationInfo (org.apache.log4j.spi.LocationInfo)2 Marker (org.apache.logging.log4j.Marker)2 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)2 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)2 JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)2