Search in sources :

Example 1 with MemoryAppender

use of org.graylog2.log4j.MemoryAppender 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)

Example 2 with MemoryAppender

use of org.graylog2.log4j.MemoryAppender in project graylog2-server by Graylog2.

the class MemoryAppenderTest method appenderIsThreadSafe.

@Test
public void appenderIsThreadSafe() throws Exception {
    final int bufferSize = 1;
    final MemoryAppender appender = MemoryAppender.createAppender(null, null, "memory", String.valueOf(bufferSize), "false");
    assertThat(appender).isNotNull();
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerName("test").setLoggerFqcn("com.example.test").setMessage(new SimpleMessage("Message")).build();
    final int threadCount = 48;
    final Thread[] threads = new Thread[threadCount];
    final TestAwareThreadGroup threadGroup = new TestAwareThreadGroup("memory-appender-test");
    final CountDownLatch latch = new CountDownLatch(1);
    for (int i = 0; i < threadCount; i++) {
        final Runnable runner = () -> {
            try {
                latch.await();
                long start = System.currentTimeMillis();
                while (System.currentTimeMillis() - start < TimeUnit.SECONDS.toMillis(4L)) {
                    appender.append(logEvent);
                }
            } catch (InterruptedException ie) {
            // Do nothing
            }
        };
        final Thread thread = new Thread(threadGroup, runner, "TestThread-" + i);
        threads[i] = thread;
        thread.start();
    }
    latch.countDown();
    for (int i = 0; i < threadCount; i++) {
        threads[i].join(TimeUnit.SECONDS.toMillis(5L));
    }
    assertThat(threadGroup.getExceptionsInThreads().get()).isEqualTo(0);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with MemoryAppender

use of org.graylog2.log4j.MemoryAppender 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)

Example 4 with MemoryAppender

use of org.graylog2.log4j.MemoryAppender in project graylog2-server by Graylog2.

the class MemoryAppenderTest method testGetLogMessages.

@Test
public void testGetLogMessages() {
    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; i++) {
        final LogEvent logEvent = Log4jLogEvent.newBuilder().setLevel(Level.INFO).setLoggerName("test").setLoggerFqcn("com.example.test").setMessage(new SimpleMessage("Message " + i)).build();
        appender.append(logEvent);
    }
    assertThat(appender.getLogMessages(bufferSize * 2)).hasSize(bufferSize);
    assertThat(appender.getLogMessages(bufferSize)).hasSize(bufferSize);
    assertThat(appender.getLogMessages(bufferSize / 2)).hasSize(bufferSize / 2);
    assertThat(appender.getLogMessages(0)).isEmpty();
    final List<LogEvent> messages = appender.getLogMessages(5);
    for (int i = 0; i < messages.size(); i++) {
        assertThat(messages.get(i).getMessage().getFormattedMessage()).isEqualTo("Message " + (bufferSize - i));
    }
}
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)4 MemoryAppender (org.graylog2.log4j.MemoryAppender)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)3 Test (org.junit.Test)3 Timed (com.codahale.metrics.annotation.Timed)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 GET (javax.ws.rs.GET)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 NotFoundException (javax.ws.rs.NotFoundException)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Level (org.apache.logging.log4j.Level)1 Marker (org.apache.logging.log4j.Marker)1 Appender (org.apache.logging.log4j.core.Appender)1 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)1 InternalLogMessage (org.graylog2.rest.models.system.loggers.responses.InternalLogMessage)1