Search in sources :

Example 6 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 7 with LogEvent

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

the class Log4JFilterTest method shouldNotFilterNonCommandLogEvent.

@Test
public void shouldNotFilterNonCommandLogEvent() {
    // given
    Message message = mockMessage(OTHER_COMMAND);
    LogEvent event = Mockito.mock(LogEvent.class);
    when(event.getMessage()).thenReturn(message);
    // when
    Result result = log4JFilter.filter(event);
    // then
    assertThat(result, equalTo(Result.NEUTRAL));
}
Also used : Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) Result(org.apache.logging.log4j.core.Filter.Result) Test(org.junit.Test)

Example 8 with LogEvent

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

the class Log4JFilterTest method shouldFilterSensitiveLogEvent.

// ---------
// Test the filter(LogEvent) method
// ---------
@Test
public void shouldFilterSensitiveLogEvent() {
    // given
    Message message = mockMessage(SENSITIVE_COMMAND);
    LogEvent event = Mockito.mock(LogEvent.class);
    when(event.getMessage()).thenReturn(message);
    // when
    Result result = log4JFilter.filter(event);
    // then
    assertThat(result, equalTo(Result.DENY));
}
Also used : Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) Result(org.apache.logging.log4j.core.Filter.Result) Test(org.junit.Test)

Example 9 with LogEvent

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

the class Log4JFilterTest method shouldNotFilterIrrelevantLogEvent.

@Test
public void shouldNotFilterIrrelevantLogEvent() {
    // given
    Message message = mockMessage(NORMAL_COMMAND);
    LogEvent event = Mockito.mock(LogEvent.class);
    when(event.getMessage()).thenReturn(message);
    // when
    Result result = log4JFilter.filter(event);
    // then
    assertThat(result, equalTo(Result.NEUTRAL));
}
Also used : Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) Result(org.apache.logging.log4j.core.Filter.Result) Test(org.junit.Test)

Example 10 with LogEvent

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

the class LoggersResource method messages.

@GET
@Timed
@ApiOperation(value = "Get recent internal log messages")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Memory appender is disabled."), @ApiResponse(code = 500, message = "Memory appender is broken.") })
@Path("/messages/recent")
@Produces(MediaType.APPLICATION_JSON)
public LogMessagesSummary messages(@ApiParam(name = "limit", value = "How many log messages should be returned", defaultValue = "500", allowableValues = "range[0, infinity]") @QueryParam("limit") @DefaultValue("500") @Min(0L) int limit, @ApiParam(name = "level", value = "Which log level (or higher) should the messages have", defaultValue = "ALL", allowableValues = "[OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL]") @QueryParam("level") @DefaultValue("ALL") @NotEmpty String level) {
    final Appender appender = getAppender(MEMORY_APPENDER_NAME);
    if (appender == null) {
        throw new NotFoundException("Memory appender is disabled. Please refer to the example log4j.xml file.");
    }
    if (!(appender instanceof MemoryAppender)) {
        throw new InternalServerErrorException("Memory appender is not an instance of MemoryAppender. Please refer to the example log4j.xml file.");
    }
    final Level logLevel = Level.toLevel(level, Level.ALL);
    final MemoryAppender memoryAppender = (MemoryAppender) appender;
    final List<InternalLogMessage> messages = new ArrayList<>(limit);
    for (LogEvent event : memoryAppender.getLogMessages(limit)) {
        final Level eventLevel = event.getLevel();
        if (!eventLevel.isMoreSpecificThan(logLevel)) {
            continue;
        }
        final ThrowableProxy thrownProxy = event.getThrownProxy();
        final String throwable;
        if (thrownProxy == null) {
            throwable = null;
        } else {
            throwable = thrownProxy.getExtendedStackTraceAsString();
        }
        final Marker marker = event.getMarker();
        messages.add(InternalLogMessage.create(event.getMessage().getFormattedMessage(), event.getLoggerName(), eventLevel.toString(), marker == null ? null : marker.toString(), new DateTime(event.getTimeMillis(), DateTimeZone.UTC), throwable, event.getThreadName(), event.getContextData().toMap()));
    }
    return LogMessagesSummary.create(messages);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) MemoryAppender(org.graylog2.log4j.MemoryAppender) MemoryAppender(org.graylog2.log4j.MemoryAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) Marker(org.apache.logging.log4j.Marker) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) DateTime(org.joda.time.DateTime) InternalLogMessage(org.graylog2.rest.models.system.loggers.responses.InternalLogMessage) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Level(org.apache.logging.log4j.Level) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

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