Search in sources :

Example 21 with SortedArrayStringMap

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

the class MapRewritePolicy method rewrite.

/**
 * {@inheritDoc}
 */
public LoggingEvent rewrite(final LoggingEvent source) {
    Object msg = source.getMessage();
    if (msg instanceof MapMessage || msg instanceof Map) {
        Map<String, String> props = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
        @SuppressWarnings("unchecked") Map<String, Object> eventProps = msg instanceof Map ? (Map) msg : ((MapMessage) msg).getData();
        // 
        // if the map sent in the logging request
        // has "message" entry, use that as the message body
        // otherwise, use the entire map.
        // 
        Message newMessage = null;
        Object newMsg = eventProps.get("message");
        if (newMsg != null) {
            newMessage = new SimpleMessage(newMsg.toString());
            for (Map.Entry<String, Object> entry : eventProps.entrySet()) {
                if (!("message".equals(entry.getKey()))) {
                    props.put(entry.getKey(), entry.getValue().toString());
                }
            }
        } else {
            return source;
        }
        LogEvent event;
        if (source instanceof LogEventAdapter) {
            event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setMessage(newMessage).setContextData(new SortedArrayStringMap(props)).build();
        } else {
            LocationInfo info = source.getLocationInformation();
            StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
            Thread thread = getThread(source.getThreadName());
            long threadId = thread != null ? thread.getId() : 0;
            int threadPriority = thread != null ? thread.getPriority() : 0;
            event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(props)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(newMessage).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
        }
        return new LogEventAdapter(event);
    } else {
        return source;
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) LogEventAdapter(org.apache.log4j.bridge.LogEventAdapter) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with SortedArrayStringMap

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

the class CaseConverterResolverTest method test_upper.

@ParameterizedTest
@CsvSource({ // case     | locale    | input         | output
"upper" + ",nl" + ",ioz" + ",IOZ", "upper" + ",nl" + ",IOZ" + ",IOZ", "lower" + ",nl" + ",ioz" + ",ioz", "lower" + ",nl" + ",IOZ" + ",ioz", "upper" + ",tr" + ",ıiğüşöç" + ",IİĞÜŞÖÇ", "upper" + ",tr" + ",IİĞÜŞÖÇ" + ",IİĞÜŞÖÇ", "lower" + ",tr" + ",ıiğüşöç" + ",ıiğüşöç", "lower" + ",tr" + ",IİĞÜŞÖÇ" + ",ıiğüşöç" })
void test_upper(final String case_, final String locale, final String input, final String output) {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("output", asMap("$resolver", "caseConverter", "case", case_, "locale", locale, "input", asMap("$resolver", "mdc", "key", "input"))));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("input", input);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> assertThat(accessor.getString("output")).isEqualTo(output));
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with SortedArrayStringMap

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

the class ReadOnlyStringMapResolverTest method test_mdc_key_access.

@Test
void test_mdc_key_access() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final StringMap contextData = new SortedArrayStringMap();
    final String mdcDirectlyAccessedKey = "mdcKey1";
    final String mdcDirectlyAccessedValue = "mdcValue1";
    contextData.putValue(mdcDirectlyAccessedKey, mdcDirectlyAccessedValue);
    final String mdcDirectlyAccessedNullPropertyKey = "mdcKey2";
    contextData.putValue(mdcDirectlyAccessedNullPropertyKey, null);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
    // Check the serialized event.
    testReadOnlyStringMapKeyAccess(mdcDirectlyAccessedKey, mdcDirectlyAccessedValue, mdcDirectlyAccessedNullPropertyKey, logEvent, "mdc");
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.jupiter.api.Test)

Example 24 with SortedArrayStringMap

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

the class ReadOnlyStringMapResolverTest method pattern_replacement_should_work.

@Test
void pattern_replacement_should_work() {
    // Create the event template.
    final String eventTemplate = writeJson(asMap("$resolver", "mdc", "pattern", "user:(role|rank)", "replacement", "$1"));
    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
    // Create the log event.
    final StringMap contextData = new SortedArrayStringMap();
    contextData.putValue("user:role", "engineer");
    contextData.putValue("user:rank", "senior");
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setContextData(contextData).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getString("role")).isEqualTo("engineer");
        assertThat(accessor.getString("rank")).isEqualTo("senior");
    });
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) Test(org.junit.jupiter.api.Test)

Example 25 with SortedArrayStringMap

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

the class JsonWriterTest method test_writeObject_IndexedReadOnlyStringMap.

@Test
void test_writeObject_IndexedReadOnlyStringMap() {
    final IndexedReadOnlyStringMap map = new SortedArrayStringMap(new LinkedHashMap<String, Object>() {

        {
            put("buzz", 1.2D);
            put("foo", "bar");
        }
    });
    final String expectedJson = "{'buzz':1.2,'foo':'bar'}".replace('\'', '"');
    final String actualJson = withLockedWriterReturning(writer -> writer.use(() -> writer.writeObject(map)));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) IndexedReadOnlyStringMap(org.apache.logging.log4j.util.IndexedReadOnlyStringMap) Test(org.junit.jupiter.api.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