Search in sources :

Example 41 with Message

use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.

the class MessageParameterResolver method resolve.

@Override
public void resolve(final LogEvent logEvent, final JsonWriter jsonWriter) {
    // If possible, perform a garbage-free resolution.
    final Message message = logEvent.getMessage();
    if (message instanceof ParameterVisitable) {
        final ParameterVisitable parameterVisitable = (ParameterVisitable) message;
        resolve(parameterVisitable, jsonWriter);
        return;
    }
    // Short-circuit if there are no parameters.
    final Object[] parameters = message.getParameters();
    if (parameters == null || parameters.length == 0 || index >= parameters.length) {
        if (index < 0) {
            jsonWriter.writeArrayStart();
            jsonWriter.writeArrayEnd();
        } else {
            jsonWriter.writeNull();
        }
        return;
    }
    // Resolve all parameters.
    if (index < 0) {
        jsonWriter.writeArrayStart();
        for (int i = 0; i < parameters.length; i++) {
            if (i > 0) {
                jsonWriter.writeSeparator();
            }
            final Object parameter = parameters[i];
            if (stringified) {
                final String stringifiedParameter = String.valueOf(parameter);
                jsonWriter.writeString(stringifiedParameter);
            } else {
                jsonWriter.writeValue(parameter);
            }
        }
        jsonWriter.writeArrayEnd();
    } else // Resolve a single parameter.
    {
        final Object parameter = parameters[index];
        if (stringified) {
            final String stringifiedParameter = String.valueOf(parameter);
            jsonWriter.writeString(stringifiedParameter);
        } else {
            jsonWriter.writeValue(parameter);
        }
    }
}
Also used : ParameterVisitable(org.apache.logging.log4j.message.ParameterVisitable) Message(org.apache.logging.log4j.message.Message)

Example 42 with Message

use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.

the class MdcPatternConverterTest method testConverterWithKeysAndPrefix.

@Test
public void testConverterWithKeysAndPrefix() {
    final Message msg = new SimpleMessage("Hello");
    final String[] options = new String[] { "object, subject" };
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
    final LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(// 
    msg).build();
    final StringBuilder sb = new StringBuilder();
    sb.append("prefix ");
    converter.format(event, sb);
    final String str = sb.toString();
    final String expected = "prefix {object=Log4j, subject=I}";
    assertEquals(expected, str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test)

Example 43 with Message

use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.

the class MdcPatternConverterTest method testConverter.

@Test
public void testConverter() {
    final Message msg = new SimpleMessage("Hello");
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(null);
    final LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(// 
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    final String expected = "{object=Log4j, subject=I, verb=love}";
    assertEquals(expected, str, "Incorrect result. Expected " + expected + ", actual " + str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test)

Example 44 with Message

use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.

the class MdcPatternConverterTest method testConverterWithKeys.

@Test
public void testConverterWithKeys() {
    final Message msg = new SimpleMessage("Hello");
    final String[] options = new String[] { "object, subject" };
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
    final LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(// 
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    final String expected = "{object=Log4j, subject=I}";
    assertEquals(expected, str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test)

Example 45 with Message

use of org.apache.logging.log4j.message.Message in project logging-log4j2 by apache.

the class MdcPatternConverterTest method testConverterWithKey.

@Test
public void testConverterWithKey() {
    final Message msg = new SimpleMessage("Hello");
    final String[] options = new String[] { "object" };
    final MdcPatternConverter converter = MdcPatternConverter.newInstance(options);
    final LogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "MyLogger").setLevel(// 
    Level.DEBUG).setMessage(// 
    msg).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String str = sb.toString();
    final String expected = "Log4j";
    assertEquals(expected, str);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.jupiter.api.Test)

Aggregations

Message (org.apache.logging.log4j.message.Message)125 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)86 LogEvent (org.apache.logging.log4j.core.LogEvent)54 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)45 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7