Search in sources :

Example 11 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 12 with StringMapMessage

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");
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Test(org.junit.jupiter.api.Test)

Example 13 with StringMapMessage

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);
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.jupiter.api.Test)

Example 14 with StringMapMessage

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);
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.jupiter.api.Test)

Example 15 with StringMapMessage

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);
}
Also used : StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) Test(org.junit.jupiter.api.Test)

Aggregations

StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)25 Test (org.junit.jupiter.api.Test)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)21 LogEvent (org.apache.logging.log4j.core.LogEvent)20 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)8 HashMap (java.util.HashMap)7 Message (org.apache.logging.log4j.message.Message)7 JsonTemplateLayout (org.apache.logging.log4j.layout.template.json.JsonTemplateLayout)5 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)4 Logger (org.apache.logging.log4j.Logger)3 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collection (java.util.Collection)1 Filter (org.apache.logging.log4j.core.Filter)1 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)1 MapMessageLookup (org.apache.logging.log4j.lookup.MapMessageLookup)1 MapMessage (org.apache.logging.log4j.message.MapMessage)1 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)1