Search in sources :

Example 31 with Log4jLogEvent

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

the class YamlLayoutTest method testMutableLogEvent.

@Test
public void testMutableLogEvent() throws Exception {
    final AbstractJacksonLayout layout = YamlLayout.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 32 with Log4jLogEvent

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

the class XmlLayoutTest method testAllFeatures.

/**
 * @param includeLocationInfo
 *            TODO
 * @param compact
 * @param includeContextMap
 *            TODO
 * @param includeContextStack
 *            TODO
 * @throws IOException
 * @throws JsonParseException
 * @throws JsonMappingException
 */
private void testAllFeatures(final boolean includeLocationInfo, final boolean compact, final boolean includeContextMap, final boolean includeContextStack, final boolean includeStacktrace) throws IOException, JsonParseException, JsonMappingException {
    final Log4jLogEvent expected = LogEventFixtures.createLogEvent();
    // @formatter:off
    final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(includeLocationInfo).setProperties(includeContextMap).setComplete(false).setCompact(compact).setIncludeStacktrace(includeStacktrace).setCharset(StandardCharsets.UTF_8).build();
    final String str = layout.toSerializable(expected);
    // @formatter:on
    // System.out.println(str);
    assertEquals(str, !compact, str.contains("\n"));
    assertEquals(str, includeLocationInfo, str.contains("Source"));
    assertEquals(str, includeContextMap, str.contains("ContextMap"));
    final Log4jLogEvent actual = new Log4jXmlObjectMapper().readValue(str, Log4jLogEvent.class);
    LogEventFixtures.assertEqualLogEvents(expected, actual, includeLocationInfo, includeContextMap, includeStacktrace);
    if (includeContextMap) {
        this.checkContextMapElement("MDC.A", "A_Value", compact, str);
        this.checkContextMapElement("MDC.B", "B_Value", compact, str);
    }
    if (includeContextStack) {
        this.checkContextStackElement("stack_msg1", compact, str);
        this.checkContextStackElement("stack_msg2", compact, str);
    }
    // 
    assertNull(actual.getThrown());
    // check some attrs
    assertTrue(str, str.contains("loggerFqcn=\"f.q.c.n\""));
    assertTrue(str, str.contains("loggerName=\"a.B\""));
    // make sure short names are used
    assertTrue(str, str.contains("<Event "));
    if (includeStacktrace) {
        assertTrue("Missing \"class=\" in: " + str, str.contains("class="));
        assertTrue("Missing \"method=\" in: " + str, str.contains("method="));
        assertTrue("Missing \"file=\" in: " + str, str.contains("file="));
        assertTrue("Missing \"line=\" in: " + str, str.contains("line="));
    }
    // 
    // make sure the names we want are used
    // this.checkAttributeName("timeMillis", compact, str);
    this.checkElementName("Instant", compact, str, true, false);
    this.checkAttributeName("epochSecond", compact, str);
    this.checkAttributeName("nanoOfSecond", compact, str);
    // and not threadName
    this.checkAttributeName("thread", compact, str);
    this.checkAttributeName("level", compact, str);
    this.checkAttributeName("loggerName", compact, str);
    this.checkElementName("Marker", compact, str, true, true);
    this.checkAttributeName("name", compact, str);
    this.checkElementName("Parents", compact, str, false, true);
    this.checkElementName("Message", compact, str, false, true);
    this.checkElementName("Thrown", compact, str, true, true);
    this.checkElementName("Cause", compact, str, true, includeStacktrace);
    this.checkAttributeName("commonElementCount", compact, str);
    this.checkAttributeName("message", compact, str);
    this.checkAttributeName("localizedMessage", compact, str);
    if (includeStacktrace) {
        this.checkElementName("ExtendedStackTrace", compact, str, false, true);
        this.checkAttributeName("class", compact, str);
        this.checkAttributeName("method", compact, str);
        this.checkAttributeName("file", compact, str);
        this.checkAttributeName("line", compact, str);
        this.checkAttributeName("exact", compact, str);
        this.checkAttributeName("location", compact, str);
        this.checkAttributeName("version", compact, str);
    } else {
        this.checkElementNameAbsent("ExtendedStackTrace", compact, str);
    }
    this.checkElementName("Suppressed", compact, str, false, true);
    this.checkAttributeName("loggerFqcn", compact, str);
    this.checkAttributeName("endOfBatch", compact, str);
    if (includeContextMap) {
        this.checkElementName("ContextMap", compact, str, false, true);
    } else {
        this.checkElementNameAbsent("ContextMap", compact, str);
    }
    this.checkElementName("ContextStack", compact, str, false, true);
    if (includeLocationInfo) {
        this.checkElementName("Source", compact, str, true, false);
    } else {
        this.checkElementNameAbsent("Source", compact, str);
    }
    // check some attrs
    this.checkAttribute("loggerFqcn", "f.q.c.n", compact, str);
    this.checkAttribute("loggerName", "a.B", compact, str);
    this.checkJsonPropertyOrder(includeContextStack, includeContextMap, includeStacktrace, str);
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) Log4jXmlObjectMapper(org.apache.logging.log4j.jackson.xml.Log4jXmlObjectMapper)

Example 33 with Log4jLogEvent

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

the class XmlLayoutTest method prepareXMLForStacktraceTests.

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

Example 34 with Log4jLogEvent

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

the class XmlLayoutTest method testLayoutLoggerName.

@Test
public void testLayoutLoggerName() {
    final XmlLayout layout = XmlLayout.newBuilder().setLocationInfo(false).setProperties(true).setComplete(true).setCompact(false).setIncludeStacktrace(true).build();
    final Log4jLogEvent event = // 
    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(event);
    assertTrue(str, str.contains("loggerName=\"a.B\""));
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 35 with Log4jLogEvent

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

the class AsyncAppender method append.

/**
 * Actual writing occurs here.
 *
 * @param logEvent The LogEvent.
 */
@Override
public void append(final LogEvent logEvent) {
    if (!isStarted()) {
        throw new IllegalStateException("AsyncAppender " + getName() + " is not active");
    }
    final Log4jLogEvent memento = Log4jLogEvent.createMemento(logEvent, includeLocation);
    InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage());
    if (!transfer(memento)) {
        if (blocking) {
            if (AbstractLogger.getRecursionDepth() > 1) {
                // LOG4J2-1518, LOG4J2-2031
                // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock
                AsyncQueueFullMessageUtil.logWarningToStatusLogger();
                logMessageInCurrentThread(logEvent);
            } else {
                // delegate to the event router (which may discard, enqueue and block, or log in current thread)
                final EventRoute route = asyncQueueFullPolicy.getRoute(dispatcher.getId(), memento.getLevel());
                route.logMessage(this, memento);
            }
        } else {
            error("Appender " + getName() + " is unable to write primary appenders. queue is full");
            logToErrorAppenderIfNecessary(false, memento);
        }
    }
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) EventRoute(org.apache.logging.log4j.core.async.EventRoute)

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