Search in sources :

Example 96 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project logging-log4j2 by apache.

the class ReusableLogEventFactoryTest method testCreateEventInitFieldsProperly.

@SuppressWarnings("deprecation")
@Test
public void testCreateEventInitFieldsProperly() throws Exception {
    final ReusableLogEventFactory factory = new ReusableLogEventFactory();
    final LogEvent event = callCreateEvent(factory, "logger", Level.INFO, new SimpleMessage("xyz"), null);
    ReusableLogEventFactory.release(event);
    assertNotNull(event.getContextMap());
    assertNotNull(event.getContextData());
    assertNotNull(event.getContextStack());
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Example 97 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project cas by apereo.

the class CasAppender method append.

@Override
public void append(final LogEvent logEvent) {
    final LogEvent newLogEvent = LoggingUtils.prepareLogEvent(logEvent);
    final String refName = this.appenderRef.getRef();
    if (StringUtils.isNotBlank(refName)) {
        final Appender appender = this.config.getAppender(refName);
        if (appender != null) {
            appender.append(newLogEvent);
        } else {
            LOGGER.warn("No log appender could be found for [{}]", refName);
        }
    } else {
        LOGGER.warn("No log appender reference could be located in logging configuration.");
    }
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) LogEvent(org.apache.logging.log4j.core.LogEvent)

Example 98 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project cas by apereo.

the class LoggingUtils method prepareLogEvent.

/**
     * Prepare log event log event.
     *
     * @param logEvent the log event
     * @return the log event
     */
public static LogEvent prepareLogEvent(final LogEvent logEvent) {
    final String messageModified = TicketIdSanitizationUtils.sanitize(logEvent.getMessage().getFormattedMessage());
    final Message message = new SimpleMessage(messageModified);
    final LogEvent newLogEvent = Log4jLogEvent.newBuilder().setLevel(logEvent.getLevel()).setLoggerName(logEvent.getLoggerName()).setLoggerFqcn(logEvent.getLoggerFqcn()).setContextMap(logEvent.getContextMap()).setContextStack(logEvent.getContextStack()).setEndOfBatch(logEvent.isEndOfBatch()).setIncludeLocation(logEvent.isIncludeLocation()).setMarker(logEvent.getMarker()).setMessage(message).setNanoTime(logEvent.getNanoTime()).setSource(logEvent.getSource()).setThreadName(logEvent.getThreadName()).setThrownProxy(logEvent.getThrownProxy()).setThrown(logEvent.getThrown()).setTimeMillis(logEvent.getTimeMillis()).build();
    return newLogEvent;
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 99 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project camel by apache.

the class BaseNettyTest method verifyNoLeaks.

@AfterClass
public static void verifyNoLeaks() throws Exception {
    //Force GC to bring up leaks
    System.gc();
    //Kick leak detection logging
    ByteBufAllocator.DEFAULT.buffer(1).release();
    Collection<LogEvent> events = LogCaptureAppender.getEvents();
    if (!events.isEmpty()) {
        String message = "Leaks detected while running tests: " + events;
        // Just write the message into log to help debug
        for (LogEvent event : events) {
            LOG.info(event.getMessage().getFormattedMessage());
        }
        LogCaptureAppender.reset();
        throw new AssertionError(message);
    }
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) AfterClass(org.junit.AfterClass)

Example 100 with LogEvent

use of org.apache.logging.log4j.core.LogEvent in project graylog2-server by Graylog2.

the class MemoryAppenderTest method appenderCanConsumeMoreMessagesThanBufferSize.

@Test
public void appenderCanConsumeMoreMessagesThanBufferSize() {
    final int bufferSize = 10;
    final MemoryAppender appender = MemoryAppender.createAppender(null, null, "memory", String.valueOf(bufferSize), "false");
    assertThat(appender).isNotNull();
    for (int i = 1; i <= bufferSize + 1; i++) {
        final LogEvent logEvent = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerName("test").setLoggerFqcn("com.example.test").setMessage(new SimpleMessage("Message " + i)).build();
        appender.append(logEvent);
    }
    final List<LogEvent> messages = appender.getLogMessages(bufferSize);
    for (int i = 0; i < messages.size(); i++) {
        assertThat(messages.get(i).getMessage().getFormattedMessage()).isEqualTo("Message " + (bufferSize - i + 1));
    }
}
Also used : MemoryAppender(org.graylog2.log4j.MemoryAppender) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Test(org.junit.Test)

Aggregations

LogEvent (org.apache.logging.log4j.core.LogEvent)188 Test (org.junit.Test)150 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)127 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)94 Message (org.apache.logging.log4j.message.Message)33 Marker (org.apache.logging.log4j.Marker)16 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)11 HashMap (java.util.HashMap)9 LoggerContext (org.apache.logging.log4j.core.LoggerContext)8 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)8 File (java.io.File)7 IOException (java.io.IOException)7 Level (org.apache.logging.log4j.Level)7 MapMessage (org.apache.logging.log4j.message.MapMessage)6 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)5 Appender (org.apache.logging.log4j.core.Appender)5 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)5 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5