Search in sources :

Example 1 with Marker

use of org.apache.logging.log4j.Marker 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 Marker

use of org.apache.logging.log4j.Marker in project geode by apache.

the class FastLoggerJUnitTest method setUp.

@Before
public void setUp() {
    this.messageFactory = new ParameterizedMessageFactory();
    this.mockedLogger = mock(ExtendedLogger.class);
    this.mockedMarker = mock(Marker.class);
    when(this.mockedLogger.getMessageFactory()).thenReturn(this.messageFactory);
    when(this.mockedMarker.getName()).thenReturn("MARKER");
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) Marker(org.apache.logging.log4j.Marker) ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) Before(org.junit.Before)

Example 3 with Marker

use of org.apache.logging.log4j.Marker 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.currentTimeMillis(), nanoClock.nanoTime());
}
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 4 with Marker

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

the class FlumePersistentAppenderTest method testSingle.

@Test
public void testSingle() throws IOException {
    final Logger logger = LogManager.getLogger("EventLogger");
    final Marker marker = MarkerManager.getMarker("EVENT");
    logger.info(marker, "This is a test message");
    final Event event = primary.poll();
    Assert.assertNotNull(event);
    final String body = getBody(event);
    Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith("This is a test message"));
}
Also used : Event(org.apache.flume.Event) AvroFlumeEvent(org.apache.flume.source.avro.AvroFlumeEvent) Marker(org.apache.logging.log4j.Marker) EventLogger(org.apache.logging.log4j.EventLogger) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.Test)

Example 5 with Marker

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

the class LevelTest method testLevelLogging.

@Test
public void testLevelLogging() {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            // Message
            logger.log(level, msg);
            // Object
            logger.log(level, 123);
            // String
            logger.log(level, name);
            // Marker, Message
            logger.log(level, marker, msg);
            // Marker, Object
            logger.log(level, marker, 123);
            // marker, String
            logger.log(level, marker, name);
            // Message, Throwable
            logger.log(level, msg, t);
            // Object, Throwable
            logger.log(level, 123, t);
            // String, Object...
            logger.log(level, name, "param1", "param2");
            // String, Throwable
            logger.log(level, name, t);
            // Marker, Message, Throwable
            logger.log(level, marker, msg, t);
            // Marker, Object, Throwable
            logger.log(level, marker, 123, t);
            // Marker, String, Object...
            logger.log(level, marker, name, "param1", "param2");
            // Marker, String, Throwable
            logger.log(level, marker, name, t);
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;
    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { //
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
    new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
    new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
    new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
    new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
    new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
    new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(description + ": No events", events);
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(), event.getLevel().name(), expected.expectedInitialEventLevel);
    }
}
Also used : ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.Logger) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Level(org.apache.logging.log4j.Level) Test(org.junit.Test)

Aggregations

Marker (org.apache.logging.log4j.Marker)50 Test (org.junit.Test)30 Message (org.apache.logging.log4j.message.Message)23 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)22 Level (org.apache.logging.log4j.Level)18 LogEvent (org.apache.logging.log4j.core.LogEvent)16 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)14 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)8 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)6 ReusableMessage (org.apache.logging.log4j.message.ReusableMessage)6 IOException (java.io.IOException)5 ClockFactoryTest (org.apache.logging.log4j.core.util.ClockFactoryTest)5 ReusableObjectMessage (org.apache.logging.log4j.message.ReusableObjectMessage)5 MutableThreadContextStack (org.apache.logging.log4j.spi.MutableThreadContextStack)5 StringMap (org.apache.logging.log4j.util.StringMap)5 ThreadContext (org.apache.logging.log4j.ThreadContext)4 Logger (org.apache.logging.log4j.core.Logger)4 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)4 HashMap (java.util.HashMap)3 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)3