Search in sources :

Example 21 with Message

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

the class Log4jLogEventTest method testEquals.

@SuppressWarnings("deprecation")
@Test
public void testEquals() {
    final StringMap contextData = ContextDataFactory.createContextData();
    contextData.putValue("A", "B");
    ThreadContext.push("first");
    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 = builder(event).build();
    assertEquals(event2, event, "copy constructor builder");
    assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
    assertEquals(contextData, event2.getContextData());
    assertSame(contextStack, event2.getContextStack());
    assertTrue(event2.isEndOfBatch());
    assertTrue(event2.isIncludeLocation());
    assertSame(Level.FATAL, event2.getLevel());
    assertSame(fqcn, event2.getLoggerFqcn());
    assertSame(name, event2.getLoggerName());
    assertSame(marker, event2.getMarker());
    assertSame(message, event2.getMessage());
    assertEquals(1234567890L, event2.getNanoTime());
    assertSame(stackTraceElement, event2.getSource());
    assertSame(threadName, event2.getThreadName());
    assertSame(exception, event2.getThrown());
    assertEquals(987654321L, event2.getTimeMillis());
    final StringMap differentMap = ContextDataFactory.emptyFrozenContextData();
    different("different contextMap", builder(event).setContextData(differentMap), event);
    different("null contextMap", builder(event).setContextData(null), event);
    ThreadContext.push("abc");
    final ContextStack contextStack2 = ThreadContext.getImmutableStack();
    different("different contextStack", builder(event).setContextStack(contextStack2), event);
    different("null contextStack", builder(event).setContextStack(null), event);
    different("different EndOfBatch", builder(event).setEndOfBatch(false), event);
    different("different IncludeLocation", builder(event).setIncludeLocation(false), event);
    different("different level", builder(event).setLevel(Level.INFO), event);
    different("null level", builder(event).setLevel(null), event);
    different("different fqcn", builder(event).setLoggerFqcn("different"), event);
    different("null fqcn", builder(event).setLoggerFqcn(null), event);
    different("different name", builder(event).setLoggerName("different"), event);
    assertThrows(NullPointerException.class, () -> different("null name", builder(event).setLoggerName(null), event));
    different("different marker", builder(event).setMarker(MarkerManager.getMarker("different")), event);
    different("null marker", builder(event).setMarker(null), event);
    different("different message", builder(event).setMessage(new ObjectMessage("different")), event);
    assertThrows(NullPointerException.class, () -> different("null message", builder(event).setMessage(null), event));
    different("different nanoTime", builder(event).setNanoTime(135), event);
    different("different milliTime", builder(event).setTimeMillis(137), event);
    final StackTraceElement stack2 = new StackTraceElement("XXX", "YYY", "file", 123);
    different("different source", builder(event).setSource(stack2), event);
    different("null source", builder(event).setSource(null), event);
    different("different threadname", builder(event).setThreadName("different"), event);
    different("null threadname", builder(event).setThreadName(null), event);
    different("different exception", builder(event).setThrown(new Error("Boo!")), event);
    different("null exception", builder(event).setThrown(null), event);
}
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) ReusableObjectMessage(org.apache.logging.log4j.message.ReusableObjectMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) 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 22 with Message

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

the class Log4jLogEventTest method testJavaIoSerializableWithUnknownThrowable.

// DO NOT REMOVE THIS COMMENT:
// UNCOMMENT WHEN GENERATING SERIALIZED EVENT FOR #testJavaIoSerializableWithUnknownThrowable
// public static class DeletedException extends Exception {
// private static final long serialVersionUID = 1L;
// public DeletedException(String msg) {
// super(msg);
// }
// };
@Test
public void testJavaIoSerializableWithUnknownThrowable() throws Exception {
    final String loggerName = "some.test";
    final Marker marker = null;
    final String loggerFQN = Strings.EMPTY;
    final Level level = Level.INFO;
    final Message msg = new SimpleMessage("abc");
    final String threadName = Thread.currentThread().getName();
    final String errorMessage = "OMG I've been deleted!";
    // DO NOT DELETE THIS COMMENT:
    // UNCOMMENT TO RE-GENERATE SERIALIZED EVENT WHEN UPDATING THIS TEST.
    // final Exception thrown = new DeletedException(errorMessage);
    // final Log4jLogEvent evt = new Log4jLogEvent(loggerName, marker, loggerFQN, level, msg, thrown);
    // final byte[] binary = serialize(evt);
    // String base64Str = DatatypeConverter.printBase64Binary(binary);
    // System.out.println("final String base64 = \"" + base64Str.replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") +
    // "\";");
    final String base64 = "rO0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xAH4ARHEAfgBFcQB+ADtzcQB+ACVzcQB+ACgBdAALaWRlYV9ydC5qYXJxAH4AK3NxAH4ALAAAAJN0AC1jb20uaW50ZWxsaWoucnQuZXhlY3V0aW9uLmFwcGxpY2F0aW9uLkFwcE1haW50AAxBcHBNYWluLmphdmFxAH4AunQAFk9NRyBJJ3ZlIGJlZW4gZGVsZXRlZCFxAH4AzXQARW9yZy5hcGFjaGUubG9nZ2luZy5sb2c0ai5jb3JlLmltcGwuTG9nNGpMb2dFdmVudFRlc3QkRGVsZXRlZEV4Y2VwdGlvbnVyADRbTG9yZy5hcGFjaGUubG9nZ2luZy5sb2c0ai5jb3JlLmltcGwuVGhyb3dhYmxlUHJveHk7+u0B4IWi6zkCAAB4cAAAAAB4";
    final byte[] binaryDecoded = decoder.decode(base64);
    final Log4jLogEvent evt2 = deserialize(binaryDecoded);
    assertEquals(loggerFQN, evt2.getLoggerFqcn());
    assertEquals(level, evt2.getLevel());
    assertEquals(loggerName, evt2.getLoggerName());
    assertEquals(marker, evt2.getMarker());
    assertEquals(msg, evt2.getMessage());
    assertEquals(threadName, evt2.getThreadName());
    assertNull(evt2.getThrown());
    assertEquals(this.getClass().getName() + "$DeletedException", evt2.getThrownProxy().getName());
    assertEquals(errorMessage, evt2.getThrownProxy().getMessage());
}
Also used : 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) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) ClockFactoryTest(org.apache.logging.log4j.core.time.ClockFactoryTest) Test(org.junit.jupiter.api.Test)

Example 23 with Message

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

the class MutableLogEventTest method testInitFromReusableCopiesFormatString.

@Test
public void testInitFromReusableCopiesFormatString() {
    Message message = ReusableMessageFactory.INSTANCE.newMessage("msg in a {}", "bottle");
    final Log4jLogEvent source = // 
    Log4jLogEvent.newBuilder().setContextData(// 
    CONTEXT_DATA).setContextStack(// 
    STACK).setEndOfBatch(// 
    true).setIncludeLocation(// 
    true).setLevel(// 
    Level.FATAL).setLoggerFqcn(// 
    "a.b.c.d.e").setLoggerName(// 
    "my name is Logger").setMarker(// 
    MarkerManager.getMarker("on your marks")).setMessage(// 
    message).setNanoTime(// 
    1234567).setSource(// 
    new StackTraceElement("myclass", "mymethod", "myfile", 123)).setThreadId(100).setThreadName("threadname").setThreadPriority(// 
    10).setThrown(// 
    new RuntimeException("run")).setTimeMillis(987654321).build();
    final MutableLogEvent mutable = new MutableLogEvent();
    mutable.initFrom(source);
    assertEquals("msg in a {}", mutable.getFormat(), "format");
    assertEquals("msg in a bottle", mutable.getFormattedMessage(), "formatted");
    assertArrayEquals(new String[] { "bottle" }, mutable.getParameters(), "parameters");
    Message memento = mutable.memento();
    assertEquals("msg in a {}", memento.getFormat(), "format");
    assertEquals("msg in a bottle", memento.getFormattedMessage(), "formatted");
    assertArrayEquals(new String[] { "bottle" }, memento.getParameters(), "parameters");
    Message eventMementoMessage = mutable.createMemento().getMessage();
    assertEquals("msg in a {}", eventMementoMessage.getFormat(), "format");
    assertEquals("msg in a bottle", eventMementoMessage.getFormattedMessage(), "formatted");
    assertArrayEquals(new String[] { "bottle" }, eventMementoMessage.getParameters(), "parameters");
    Message log4JLogEventMessage = new Log4jLogEvent.Builder(mutable).build().getMessage();
    assertEquals("msg in a {}", log4JLogEventMessage.getFormat(), "format");
    assertEquals("msg in a bottle", log4JLogEventMessage.getFormattedMessage(), "formatted");
    assertArrayEquals(new String[] { "bottle" }, log4JLogEventMessage.getParameters(), "parameters");
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ReusableSimpleMessage(org.apache.logging.log4j.message.ReusableSimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) Test(org.junit.jupiter.api.Test)

Example 24 with Message

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

the class Rfc5424Layout method appendMessage.

private void appendMessage(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    // This layout formats StructuredDataMessages instead of delegating to the Message itself.
    final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage) ? message.getFormat() : message.getFormattedMessage();
    if (text != null && text.length() > 0) {
        buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
    }
    if (exceptionFormatters != null && event.getThrown() != null) {
        final StringBuilder exception = new StringBuilder(LF);
        for (final PatternFormatter formatter : exceptionFormatters) {
            formatter.format(event, exception);
        }
        buffer.append(escapeNewlines(exception.toString(), escapeNewLine));
    }
    if (includeNewLine) {
        buffer.append(LF);
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) MessageCollectionMessage(org.apache.logging.log4j.message.MessageCollectionMessage) Message(org.apache.logging.log4j.message.Message) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) StructuredDataCollectionMessage(org.apache.logging.log4j.message.StructuredDataCollectionMessage) PatternFormatter(org.apache.logging.log4j.core.pattern.PatternFormatter) MessageCollectionMessage(org.apache.logging.log4j.message.MessageCollectionMessage)

Example 25 with Message

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

the class Log4jSystemLogger method log.

@Override
public void log(final Level level, final ResourceBundle bundle, final String format, final Object... params) {
    Message message = createMessage(getResource(bundle, format), params);
    logger.logIfEnabled(FQCN, getLevel(level), null, message, message.getThrowable());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MessageFormatMessage(org.apache.logging.log4j.message.MessageFormatMessage) Message(org.apache.logging.log4j.message.Message)

Aggregations

Message (org.apache.logging.log4j.message.Message)125 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)86 LogEvent (org.apache.logging.log4j.core.LogEvent)54 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)45 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7