Search in sources :

Example 16 with Message

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

the class AbstractLogger method printf.

@Override
public void printf(final Level level, final String format, final Object... params) {
    if (isEnabled(level, null, format, params)) {
        final Message msg = new StringFormattedMessage(format, params);
        logMessageSafely(FQCN, level, null, msg, msg.getThrowable());
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) EntryMessage(org.apache.logging.log4j.message.EntryMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage)

Example 17 with Message

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

the class AsyncLoggerConfigTest3 method testNoConcurrentModificationException.

@Test
public void testNoConcurrentModificationException() throws Exception {
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigTest2.xml");
    final File file = new File("target", "AsyncLoggerConfigTest2.log");
    assertTrue("Deleted old file before test", !file.exists() || file.delete());
    final Logger log = LogManager.getLogger("com.foo.Bar");
    log.info("initial message");
    Thread.sleep(500);
    final Map<String, String> map = new HashMap<>();
    for (int j = 0; j < 3000; j++) {
        map.put(String.valueOf(j), String.valueOf(System.nanoTime()));
    }
    final Message msg = new ParameterizedMessage("{}", map);
    Log4jLogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.WARN).setLoggerName(getClass().getName()).setMessage(msg).setTimeMillis(0).build();
    for (int i = 0; i < 100; i++) {
        ((AsyncLoggerConfig) ((org.apache.logging.log4j.core.Logger) log).get()).callAppenders(event);
        for (int j = 0; j < 3000; j++) {
            map.remove(String.valueOf(j));
        }
        for (int j = 0; j < 3000; j++) {
            map.put(String.valueOf(j), String.valueOf(System.nanoTime()));
        }
    }
}
Also used : ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) Logger(org.apache.logging.log4j.Logger) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) File(java.io.File) Test(org.junit.Test)

Example 18 with Message

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

the class AsyncLogger method translateTo.

/*
     * (non-Javadoc)
     *
     * @see com.lmax.disruptor.EventTranslatorVararg#translateTo(java.lang.Object, long, java.lang.Object[])
     */
@Override
public void translateTo(final RingBufferLogEvent event, final long sequence, final Object... args) {
    // Implementation note: candidate for optimization: exceeds 35 bytecodes.
    final AsyncLogger asyncLogger = (AsyncLogger) args[0];
    final StackTraceElement location = (StackTraceElement) args[1];
    final String fqcn = (String) args[2];
    final Level level = (Level) args[3];
    final Marker marker = (Marker) args[4];
    final Message message = (Message) args[5];
    final Throwable thrown = (Throwable) args[6];
    // needs shallow copy to be fast (LOG4J2-154)
    final ContextStack contextStack = ThreadContext.getImmutableStack();
    final Thread currentThread = Thread.currentThread();
    final String threadName = THREAD_NAME_CACHING_STRATEGY.getThreadName();
    event.setValues(asyncLogger, asyncLogger.getName(), marker, fqcn, level, message, thrown, // in the AsyncLogger#actualAsyncLog method
    CONTEXT_DATA_INJECTOR.injectContextData(null, (StringMap) event.getContextData()), contextStack, currentThread.getId(), threadName, currentThread.getPriority(), location, CLOCK, nanoClock);
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) ReusableMessage(org.apache.logging.log4j.message.ReusableMessage) Message(org.apache.logging.log4j.message.Message) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack)

Example 19 with Message

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

the class CategoryTest method testMessageImplementation.

private <M extends Message> void testMessageImplementation(final Object messagePayload, final Class<M> expectedMessageClass, final Consumer<M> messageTester) {
    // Setup the logger and the appender.
    final Category category = Category.getInstance("TestCategory");
    final org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) category.getLogger();
    logger.addAppender(appender);
    // Log the message payload.
    category.info(messagePayload);
    // Verify collected log events.
    final List<LogEvent> events = appender.getEvents();
    assertEquals("was expecting a single event", 1, events.size());
    final LogEvent logEvent = events.get(0);
    // Verify the collected message.
    final Message message = logEvent.getMessage();
    final Class<? extends Message> actualMessageClass = message.getClass();
    assertTrue("was expecting message to be instance of " + expectedMessageClass + ", found: " + actualMessageClass, expectedMessageClass.isAssignableFrom(actualMessageClass));
    @SuppressWarnings("unchecked") final M typedMessage = (M) message;
    messageTester.accept(typedMessage);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent)

Example 20 with Message

use of org.apache.logging.log4j.message.Message 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)

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