Search in sources :

Example 26 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class ReadOnlyStringMapResolverTest method test_mdc_pattern.

@Test
void test_mdc_pattern() {
    // Create the log event.
    final SimpleMessage message = new SimpleMessage("Hello, World!");
    final StringMap contextData = new SortedArrayStringMap();
    final String mdcPatternMatchedKey = "mdcKey1";
    final String mdcPatternMatchedValue = "mdcValue1";
    contextData.putValue(mdcPatternMatchedKey, mdcPatternMatchedValue);
    final String mdcPatternMismatchedKey = "mdcKey2";
    final String mdcPatternMismatchedValue = "mdcValue2";
    contextData.putValue(mdcPatternMismatchedKey, mdcPatternMismatchedValue);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
    // Check the serialized event.
    testReadOnlyStringMapPattern(mdcPatternMatchedKey, mdcPatternMatchedValue, mdcPatternMismatchedKey, logEvent, "mdc");
}
Also used : SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) StringMap(org.apache.logging.log4j.util.StringMap) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.jupiter.api.Test)

Example 27 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class JndiRestrictedLookupTest method testBadSerializableLookup.

@Test
public void testBadSerializableLookup() throws Exception {
    int port = embeddedLdapRule.embeddedServerPort();
    Context context = embeddedLdapRule.context();
    context.bind("cn=" + TEST_MESSAGE + "," + DOMAIN_DSN, new SimpleMessage("Test Message"));
    final StrLookup lookup = new JndiLookup();
    String result = lookup.lookup(LDAP_URL + port + "/" + "cn=" + TEST_MESSAGE + "," + DOMAIN_DSN);
    if (result != null) {
        fail("Lookup returned an object");
    }
}
Also used : Context(javax.naming.Context) StrLookup(org.apache.logging.log4j.core.lookup.StrLookup) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 28 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class PropertyRewritePolicy method rewrite.

/**
 * {@inheritDoc}
 */
public LoggingEvent rewrite(final LoggingEvent source) {
    if (!properties.isEmpty()) {
        Map<String, String> rewriteProps = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            if (!rewriteProps.containsKey(entry.getKey())) {
                rewriteProps.put(entry.getKey(), entry.getValue());
            }
        }
        LogEvent event;
        if (source instanceof LogEventAdapter) {
            event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setContextData(new SortedArrayStringMap(rewriteProps)).build();
        } else {
            LocationInfo info = source.getLocationInformation();
            StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
            Thread thread = getThread(source.getThreadName());
            long threadId = thread != null ? thread.getId() : 0;
            int threadPriority = thread != null ? thread.getPriority() : 0;
            event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(rewriteProps)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(new SimpleMessage(source.getRenderedMessage())).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
        }
        return new LogEventAdapter(event);
    }
    return source;
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEventAdapter(org.apache.log4j.bridge.LogEventAdapter) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.

@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
    final StringMap contextData = ContextDataFactory.createContextData();
    contextData.putValue("A", "B");
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Exception exception = new Exception("test");
    final Marker marker = MarkerManager.getMarker("EVENTTEST");
    final Message message = new SimpleMessage("foo");
    final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final Log4jLogEvent event = // 
    Log4jLogEvent.newBuilder().setContextData(// 
    contextData).setContextStack(// 
    contextStack).setEndOfBatch(// 
    true).setIncludeLocation(// 
    true).setLevel(// 
    Level.FATAL).setLoggerFqcn(// 
    fqcn).setLoggerName(// 
    name).setMarker(// 
    marker).setMessage(// 
    message).setNanoTime(// 
    1234567890L).setSource(// 
    stackTraceElement).setThreadName(// 
    threadName).setThrown(// 
    exception).setTimeMillis(987654321L).build();
    assertEquals(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertTrue(event.isEndOfBatch());
    assertTrue(event.isIncludeLocation());
    assertSame(Level.FATAL, event.getLevel());
    assertSame(fqcn, event.getLoggerFqcn());
    assertSame(name, event.getLoggerName());
    assertSame(marker, event.getMarker());
    assertSame(message, event.getMessage());
    assertEquals(1234567890L, event.getNanoTime());
    assertSame(stackTraceElement, event.getSource());
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
    assertEquals(event2, event, "copy constructor builder");
    assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Example 30 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.

the class Log4jLogEventTest method testJavaIoSerializableWithThrown.

@Test
public void testJavaIoSerializableWithThrown() throws Exception {
    final Error thrown = new InternalError("test error");
    final Log4jLogEvent evt = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "some.test").setLoggerFqcn(// 
    Strings.EMPTY).setLevel(// 
    Level.INFO).setMessage(// 
    new SimpleMessage("abc")).setThrown(// 
    thrown).build();
    final byte[] binary = serialize(evt);
    final Log4jLogEvent evt2 = deserialize(binary);
    assertEquals(evt.getTimeMillis(), evt2.getTimeMillis());
    assertEquals(evt.getLoggerFqcn(), evt2.getLoggerFqcn());
    assertEquals(evt.getLevel(), evt2.getLevel());
    assertEquals(evt.getLoggerName(), evt2.getLoggerName());
    assertEquals(evt.getMarker(), evt2.getMarker());
    assertEquals(evt.getContextData(), evt2.getContextData());
    assertEquals(evt.getContextStack(), evt2.getContextStack());
    assertEquals(evt.getMessage(), evt2.getMessage());
    assertEquals(evt.getSource(), evt2.getSource());
    assertEquals(evt.getThreadName(), evt2.getThreadName());
    assertNull(evt2.getThrown());
    assertNotNull(evt2.getThrownProxy());
    assertEquals(evt.getThrownProxy(), evt2.getThrownProxy());
    assertEquals(evt.isEndOfBatch(), evt2.isEndOfBatch());
    assertEquals(evt.isIncludeLocation(), evt2.isIncludeLocation());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)198 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)148 LogEvent (org.apache.logging.log4j.core.LogEvent)142 Test (org.junit.jupiter.api.Test)128 Message (org.apache.logging.log4j.message.Message)43 Marker (org.apache.logging.log4j.Marker)23 Test (org.junit.Test)21 StringMap (org.apache.logging.log4j.util.StringMap)20 Level (org.apache.logging.log4j.Level)17 IOException (java.io.IOException)13 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)12 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 LoggerContext (org.apache.logging.log4j.core.LoggerContext)10 ClockFactoryTest (org.apache.logging.log4j.core.time.ClockFactoryTest)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4