Search in sources :

Example 51 with Message

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

the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributesInclContextData.

@Test
public void testBuilderCorrectlyCopiesAllEventAttributesInclContextData() {
    final StringMap contextData = new SortedArrayStringMap();
    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();
    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertEquals(true, event.isEndOfBatch());
    assertEquals(true, 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("copy constructor builder", event2, event);
    assertEquals("same hashCode", event2.hashCode(), event.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) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) IOException(java.io.IOException) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 52 with Message

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

the class Log4jLogEventTest method testBuilderCorrectlyCopiesMutableLogEvent.

@Test
public void testBuilderCorrectlyCopiesMutableLogEvent() throws Exception {
    final StringMap contextData = new SortedArrayStringMap();
    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");
    new StackTraceElement("A", "B", "file", 123);
    final String fqcn = "qualified";
    final String name = "Ceci n'est pas une pipe";
    final String threadName = "threadName";
    final MutableLogEvent event = new MutableLogEvent();
    event.setContextData(contextData);
    event.setContextStack(contextStack);
    event.setEndOfBatch(true);
    event.setIncludeLocation(true);
    //event.setSource(stackTraceElement); // cannot be explicitly set
    event.setLevel(Level.FATAL);
    event.setLoggerFqcn(fqcn);
    event.setLoggerName(name);
    event.setMarker(marker);
    event.setMessage(message);
    event.setNanoTime(1234567890L);
    event.setThreadName(threadName);
    event.setThrown(exception);
    event.setTimeMillis(987654321L);
    assertSame(contextData, event.getContextData());
    assertSame(contextStack, event.getContextStack());
    assertEquals(true, event.isEndOfBatch());
    assertEquals(true, 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()); // don't invoke
    assertSame(threadName, event.getThreadName());
    assertSame(exception, event.getThrown());
    assertEquals(987654321L, event.getTimeMillis());
    final LogEvent e2 = new Log4jLogEvent.Builder(event).build();
    assertEquals(contextData, e2.getContextData());
    assertSame(contextStack, e2.getContextStack());
    assertEquals(true, e2.isEndOfBatch());
    assertEquals(true, e2.isIncludeLocation());
    assertSame(Level.FATAL, e2.getLevel());
    assertSame(fqcn, e2.getLoggerFqcn());
    assertSame(name, e2.getLoggerName());
    assertSame(marker, e2.getMarker());
    assertSame(message, e2.getMessage());
    assertEquals(1234567890L, e2.getNanoTime());
    //assertSame(stackTraceElement, e2.getSource()); // don't invoke
    assertSame(threadName, e2.getThreadName());
    assertSame(exception, e2.getThrown());
    assertEquals(987654321L, e2.getTimeMillis());
    // use reflection to get value of source field in log event copy:
    // invoking the getSource() method would initialize the field
    final Field fieldSource = Log4jLogEvent.class.getDeclaredField("source");
    fieldSource.setAccessible(true);
    final Object value = fieldSource.get(e2);
    assertNull("source in copy", value);
}
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) Field(java.lang.reflect.Field) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Test(org.junit.Test) ClockFactoryTest(org.apache.logging.log4j.core.util.ClockFactoryTest)

Example 53 with Message

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

the class PatternLayoutComparisonBenchmark method createLog4j2Event.

private static LogEvent createLog4j2Event() {
    final Marker marker = null;
    final String fqcn = "com.mycom.myproject.mypackage.MyClass";
    final Level level = Level.DEBUG;
    final Message message = new SimpleMessage(STR);
    final Throwable t = null;
    final Map<String, String> mdc = null;
    final ContextStack ndc = null;
    final String threadName = null;
    final StackTraceElement location = null;
    final long timestamp = 12345678;
    return //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "name(ignored)").setMarker(//
    marker).setLoggerFqcn(//
    fqcn).setLevel(//
    level).setMessage(//
    message).setThrown(//
    t).setContextMap(//
    mdc).setContextStack(//
    ndc).setThreadName(//
    threadName).setSource(//
    location).setTimeMillis(//
    timestamp).build();
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) 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) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack)

Example 54 with Message

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

the class RingBufferLogEventTest method testGetMillisReturnsConstructorMillisForNormalMessage.

@Test
public void testGetMillisReturnsConstructorMillisForNormalMessage() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = null;
    final Marker marker = null;
    final String fqcn = null;
    final Level level = null;
    final Message data = null;
    final Throwable t = null;
    final ContextStack contextStack = null;
    final String threadName = null;
    final StackTraceElement location = null;
    final long currentTimeMillis = 123;
    final long nanoTime = 1;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, currentTimeMillis, nanoTime);
    assertEquals(123, evt.getTimeMillis());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.Test)

Example 55 with Message

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

the class LoggingMessageTagSupport method doEndTag.

@Override
public final int doEndTag() throws JspException {
    final Log4jTaglibLogger logger = this.getLogger();
    final Level level = this.getLevel();
    final Marker marker = this.getMarker();
    if (TagUtils.isEnabled(logger, level, marker)) {
        final Object message = this.getMessage();
        final Throwable exception = this.getException();
        if (message instanceof Message) {
            logger.logIfEnabled(FQCN, level, marker, (Message) message, exception);
        } else if (message instanceof String) {
            Message data;
            if (this.attributes.size() > 0) {
                data = logger.getMessageFactory().newMessage((String) message, this.attributes.toArray());
            } else {
                data = logger.getMessageFactory().newMessage((String) message);
            }
            logger.logIfEnabled(FQCN, level, marker, data, exception);
        } else {
            logger.logIfEnabled(FQCN, level, marker, logger.getMessageFactory().newMessage(message), exception);
        }
    }
    return Tag.EVAL_PAGE;
}
Also used : Message(org.apache.logging.log4j.message.Message) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker)

Aggregations

Message (org.apache.logging.log4j.message.Message)80 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)45 Test (org.junit.Test)42 LogEvent (org.apache.logging.log4j.core.LogEvent)33 Marker (org.apache.logging.log4j.Marker)23 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)22 Level (org.apache.logging.log4j.Level)17 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)14 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)13 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)8 Result (org.apache.logging.log4j.core.Filter.Result)7 LocalizedMessage (org.apache.logging.log4j.message.LocalizedMessage)6 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)6 IOException (java.io.IOException)5 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)5