Search in sources :

Example 81 with SimpleMessage

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

the class MutableLogEventTest method testInitFromCopiesAllFields.

@Test
public void testInitFromCopiesAllFields() {
    //        private ThrowableProxy thrownProxy;
    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(//
    new SimpleMessage("msg in a bottle")).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("contextMap", CONTEXT_DATA, mutable.getContextData());
    assertEquals("stack", STACK, mutable.getContextStack());
    assertEquals("endOfBatch", true, mutable.isEndOfBatch());
    assertEquals("IncludeLocation()", true, mutable.isIncludeLocation());
    assertEquals("level", Level.FATAL, mutable.getLevel());
    assertEquals("LoggerFqcn()", source.getLoggerFqcn(), mutable.getLoggerFqcn());
    assertEquals("LoggerName", source.getLoggerName(), mutable.getLoggerName());
    assertEquals("marker", source.getMarker(), mutable.getMarker());
    assertEquals("msg", source.getMessage(), mutable.getMessage());
    assertEquals("nano", source.getNanoTime(), mutable.getNanoTime());
    assertEquals("src", source.getSource(), mutable.getSource());
    assertEquals("tid", source.getThreadId(), mutable.getThreadId());
    assertEquals("tname", source.getThreadName(), mutable.getThreadName());
    assertEquals("tpriority", source.getThreadPriority(), mutable.getThreadPriority());
    assertEquals("throwns", source.getThrown(), mutable.getThrown());
    assertEquals("proxy", source.getThrownProxy(), mutable.getThrownProxy());
    assertEquals("millis", source.getTimeMillis(), mutable.getTimeMillis());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 82 with SimpleMessage

use of org.apache.logging.log4j.message.SimpleMessage 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 83 with SimpleMessage

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

the class ReusableLogEventFactoryTest method testCreateEventReturnsThreadLocalInstance.

@Test
public void testCreateEventReturnsThreadLocalInstance() throws Exception {
    final ReusableLogEventFactory factory = new ReusableLogEventFactory();
    final LogEvent[] event1 = new LogEvent[1];
    final LogEvent[] event2 = new LogEvent[1];
    final Thread t1 = new Thread("THREAD 1") {

        @Override
        public void run() {
            event1[0] = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
        }
    };
    final Thread t2 = new Thread("Thread 2") {

        @Override
        public void run() {
            event2[0] = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
        }
    };
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    assertNotNull(event1[0]);
    assertNotNull(event2[0]);
    assertNotSame(event1[0], event2[0]);
    assertEquals("logger", "a", event1[0].getLoggerName());
    assertEquals("level", Level.DEBUG, event1[0].getLevel());
    assertEquals("msg", new SimpleMessage("abc"), event1[0].getMessage());
    assertEquals("thread name", "THREAD 1", event1[0].getThreadName());
    assertEquals("tid", t1.getId(), event1[0].getThreadId());
    assertEquals("logger", "b", event2[0].getLoggerName());
    assertEquals("level", Level.INFO, event2[0].getLevel());
    assertEquals("msg", new SimpleMessage("xyz"), event2[0].getMessage());
    assertEquals("thread name", "Thread 2", event2[0].getThreadName());
    assertEquals("tid", t2.getId(), event2[0].getThreadId());
    ReusableLogEventFactory.release(event1[0]);
    ReusableLogEventFactory.release(event2[0]);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 84 with SimpleMessage

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

the class ReusableLogEventFactoryTest method testCreateEventReturnsDifferentInstanceIfNotReleased.

@Test
public void testCreateEventReturnsDifferentInstanceIfNotReleased() throws Exception {
    final ReusableLogEventFactory factory = new ReusableLogEventFactory();
    final LogEvent event1 = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
    final LogEvent event2 = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
    assertNotSame(event1, event2);
    ReusableLogEventFactory.release(event1);
    ReusableLogEventFactory.release(event2);
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 85 with SimpleMessage

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

the class ReusableLogEventFactoryTest method testCreateEventOverwritesFields.

@Test
public void testCreateEventOverwritesFields() throws Exception {
    final ReusableLogEventFactory factory = new ReusableLogEventFactory();
    final LogEvent event1 = callCreateEvent(factory, "a", Level.DEBUG, new SimpleMessage("abc"), null);
    assertEquals("logger", "a", event1.getLoggerName());
    assertEquals("level", Level.DEBUG, event1.getLevel());
    assertEquals("msg", new SimpleMessage("abc"), event1.getMessage());
    ReusableLogEventFactory.release(event1);
    final LogEvent event2 = callCreateEvent(factory, "b", Level.INFO, new SimpleMessage("xyz"), null);
    assertSame(event1, event2);
    assertEquals("logger", "b", event1.getLoggerName());
    assertEquals("level", Level.INFO, event1.getLevel());
    assertEquals("msg", new SimpleMessage("xyz"), event1.getMessage());
    assertEquals("logger", "b", event2.getLoggerName());
    assertEquals("level", Level.INFO, event2.getLevel());
    assertEquals("msg", new SimpleMessage("xyz"), event2.getMessage());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)130 Test (org.junit.Test)98 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)96 LogEvent (org.apache.logging.log4j.core.LogEvent)93 Message (org.apache.logging.log4j.message.Message)34 Marker (org.apache.logging.log4j.Marker)19 Level (org.apache.logging.log4j.Level)11 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)10 IOException (java.io.IOException)7 LoggerContext (org.apache.logging.log4j.core.LoggerContext)7 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)6 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 HashMap (java.util.HashMap)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4