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);
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations