Search in sources :

Example 16 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class YamlLayoutTest method testLayoutLoggerName.

@Test
public void testLayoutLoggerName() throws Exception {
    final AbstractJacksonLayout layout = YamlLayout.newBuilder().setLocationInfo(false).setProperties(false).setIncludeStacktrace(true).setCharset(StandardCharsets.UTF_8).build();
    final Log4jLogEvent expected = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "a.B").setLoggerFqcn(// 
    "f.q.c.n").setLevel(// 
    Level.DEBUG).setMessage(// 
    new SimpleMessage("M")).setThreadName(// 
    "threadName").setTimeMillis(1).build();
    final String str = layout.toSerializable(expected);
    assertThat(str, containsString("loggerName: \"a.B\""));
    final Log4jLogEvent actual = new Log4jYamlObjectMapper(false, true, false).readValue(str, Log4jLogEvent.class);
    assertEquals(expected.getLoggerName(), actual.getLoggerName());
    assertEquals(expected, actual);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jYamlObjectMapper(org.apache.logging.log4j.jackson.yaml.Log4jYamlObjectMapper) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) Test(org.junit.Test)

Example 17 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class YamlLayoutTest method prepareYAMLForStacktraceTests.

private String prepareYAMLForStacktraceTests(final boolean stacktraceAsString) {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    // @formatter:off
    final AbstractJacksonLayout layout = YamlLayout.newBuilder().setIncludeStacktrace(true).setStacktraceAsString(stacktraceAsString).build();
    // @formatter:on
    return layout.toSerializable(expected);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout)

Example 18 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class XmlLayoutTest method testMutableLogEvent.

@Test
public void testMutableLogEvent() throws Exception {
    final AbstractJacksonLayout layout = XmlLayout.newBuilder().setLocationInfo(false).setProperties(false).setIncludeStacktrace(false).setAdditionalFields(new KeyValuePair[] { new KeyValuePair("KEY1", "VALUE1"), new KeyValuePair("KEY2", "${java:runtime}") }).setCharset(StandardCharsets.UTF_8).setConfiguration(ctx.getConfiguration()).build();
    Log4jLogEvent logEvent = LogEventFixtures.createLogEvent();
    final MutableLogEvent mutableEvent = new MutableLogEvent();
    mutableEvent.initFrom(logEvent);
    final String strLogEvent = layout.toSerializable(logEvent);
    final String strMutableEvent = layout.toSerializable(mutableEvent);
    assertEquals(strMutableEvent, strLogEvent, strMutableEvent);
}
Also used : KeyValuePair(org.apache.logging.log4j.core.util.KeyValuePair) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) AbstractJacksonLayout(org.apache.logging.log4j.jackson.AbstractJacksonLayout) MutableLogEvent(org.apache.logging.log4j.core.impl.MutableLogEvent) Test(org.junit.Test)

Example 19 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class Log4j1XmlLayoutTest method testWithoutThrown.

@Test
public void testWithoutThrown() {
    final Log4j1XmlLayout layout = Log4j1XmlLayout.createLayout(false, true);
    final Log4jLogEvent event = Log4jLogEvent.newBuilder().setLoggerName("a.B").setLevel(Level.INFO).setMessage(new SimpleMessage("Hello, World")).setTimeMillis(System.currentTimeMillis() + 17).build();
    final String result = layout.toSerializable(event);
    final String expected = "<log4j:event logger=\"a.B\" timestamp=\"" + event.getTimeMillis() + "\" level=\"INFO\" thread=\"main\">\r\n" + "<log4j:message><![CDATA[Hello, World]]></log4j:message>\r\n" + "</log4j:event>\r\n\r\n";
    assertEquals(expected, result);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 20 with Log4jLogEvent

use of org.apache.logging.log4j.core.impl.Log4jLogEvent in project logging-log4j2 by apache.

the class ExtendedThrowablePatternConverterTest method testDeserializedLogEventWithThrowableProxyButNoThrowable.

@Test
public void testDeserializedLogEventWithThrowableProxyButNoThrowable() {
    final ExtendedThrowablePatternConverter converter = ExtendedThrowablePatternConverter.newInstance(null, null);
    final Throwable originalThrowable = new Exception("something bad happened");
    final ThrowableProxy throwableProxy = new ThrowableProxy(originalThrowable);
    final Throwable deserializedThrowable = null;
    final Log4jLogEvent event = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "testLogger").setLoggerFqcn(// 
    this.getClass().getName()).setLevel(// 
    Level.DEBUG).setMessage(// 
    new SimpleMessage("")).setThrown(// 
    deserializedThrowable).setThrownProxy(// 
    throwableProxy).setTimeMillis(0).build();
    final StringBuilder sb = new StringBuilder();
    converter.format(event, sb);
    final String result = sb.toString();
    assertTrue(result.contains(originalThrowable.getMessage()), result);
    assertTrue(result.contains(originalThrowable.getStackTrace()[0].getMethodName()), result);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) Test(org.junit.jupiter.api.Test)

Aggregations

Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)37 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)15 Test (org.junit.Test)15 AbstractJacksonLayout (org.apache.logging.log4j.jackson.AbstractJacksonLayout)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 MutableLogEvent (org.apache.logging.log4j.core.impl.MutableLogEvent)5 Log4jJsonObjectMapper (org.apache.logging.log4j.jackson.json.Log4jJsonObjectMapper)5 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 StringMap (org.apache.logging.log4j.util.StringMap)4 Serializable (java.io.Serializable)3 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)3 Message (org.apache.logging.log4j.message.Message)3 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)3 Test (org.junit.jupiter.api.Test)3 Benchmark (org.openjdk.jmh.annotations.Benchmark)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Marker (org.apache.logging.log4j.Marker)2 Log4jJsonObjectMapper (org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper)2