use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MessagePatternConverterTest method testMapMessageFormatDefault.
@Test
public void testMapMessageFormatDefault() {
final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, null);
Message msg = new StringMapMessage().with("key", "val");
LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(msg).build();
StringBuilder sb = new StringBuilder();
converter.format(event, sb);
assertEquals("key=\"val\"", sb.toString(), "Unexpected result");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MessagePatternConverterTest method testMapMessageFormatXml.
@Test
public void testMapMessageFormatXml() {
final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, new String[] { "xml" });
Message msg = new StringMapMessage().with("key", "val");
LogEvent event = //
Log4jLogEvent.newBuilder().setLoggerName(//
"MyLogger").setLevel(//
Level.DEBUG).setMessage(msg).build();
StringBuilder sb = new StringBuilder();
converter.format(event, sb);
assertEquals("<Map>\n <Entry key=\"key\">val</Entry>\n</Map>", sb.toString(), "Unexpected result");
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapPatternConverterTest method testConverterWithJavaFormat.
@Test
public void testConverterWithJavaFormat() {
final StringMapMessage msg = new StringMapMessage();
msg.put("subject", "I");
msg.put("verb", "love");
msg.put("object", "Log4j");
final MapPatternConverter converter = MapPatternConverter.newInstance(null, MapMessage.MapFormat.JAVA);
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();
String expected = "subject=\"I\"";
assertTrue(str.contains(expected), "Missing or incorrect subject. Expected " + expected + ", actual " + str);
expected = "verb=\"love\"";
assertTrue(str.contains(expected), "Missing or incorrect verb");
expected = "object=\"Log4j\"";
assertTrue(str.contains(expected), "Missing or incorrect object");
assertEquals("{object=\"Log4j\", subject=\"I\", verb=\"love\"}", str);
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapPatternConverterTest method testConverter.
@Test
public void testConverter() {
final StringMapMessage msg = new StringMapMessage();
msg.put("subject", "I");
msg.put("verb", "love");
msg.put("object", "Log4j");
final MapPatternConverter converter = MapPatternConverter.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();
String expected = "subject=I";
assertTrue(str.contains(expected), "Missing or incorrect subject. Expected " + expected + ", actual " + str);
expected = "verb=love";
assertTrue(str.contains(expected), "Missing or incorrect verb");
expected = "object=Log4j";
assertTrue(str.contains(expected), "Missing or incorrect object");
assertEquals("{object=Log4j, subject=I, verb=love}", str);
}
use of org.apache.logging.log4j.message.StringMapMessage in project logging-log4j2 by apache.
the class MapPatternConverterTest method testConverterWithKey.
@Test
public void testConverterWithKey() {
final StringMapMessage msg = new StringMapMessage();
msg.put("subject", "I");
msg.put("verb", "love");
msg.put("object", "Log4j");
final MapPatternConverter converter = MapPatternConverter.newInstance(new String[] { "object" });
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