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);
}
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");
}
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());
}
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"));
}
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);
}
}
Aggregations