use of org.apache.logging.log4j.core.Appender 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.core.Appender in project geode by apache.
the class LogService method removeConsoleAppender.
/**
* Removes STDOUT ConsoleAppender from ROOT logger. Only called when using the log4j2-default.xml
* configuration. This is done when creating the LogWriterAppender for log-file. The Appender
* instance is stored in stdoutAppender so it can be restored later using restoreConsoleAppender.
*/
public static synchronized void removeConsoleAppender() {
final AppenderContext appenderContext = LogService.getAppenderContext(LogService.ROOT_LOGGER_NAME);
final LoggerConfig config = appenderContext.getLoggerConfig();
Appender stdout = config.getAppenders().get(STDOUT);
if (stdout != null) {
config.removeAppender(STDOUT);
stdoutAppender = stdout;
appenderContext.getLoggerContext().updateLoggers();
}
}
use of org.apache.logging.log4j.core.Appender in project geode by apache.
the class LogService method restoreConsoleAppender.
/**
* Restores STDOUT ConsoleAppender to ROOT logger. Only called when using the log4j2-default.xml
* configuration. This is done when the LogWriterAppender for log-file is destroyed. The Appender
* instance stored in stdoutAppender is used.
*/
public static synchronized void restoreConsoleAppender() {
if (stdoutAppender == null) {
return;
}
final AppenderContext appenderContext = LogService.getAppenderContext(LogService.ROOT_LOGGER_NAME);
final LoggerConfig config = appenderContext.getLoggerConfig();
Appender stdout = config.getAppenders().get(STDOUT);
if (stdout == null) {
config.addAppender(stdoutAppender, Level.ALL, null);
appenderContext.getLoggerContext().updateLoggers();
}
}
use of org.apache.logging.log4j.core.Appender in project geode by apache.
the class LogServiceIntegrationJUnitTest method removeAndRestoreConsoleAppenderShouldAffectRootLogger.
@Test
public void removeAndRestoreConsoleAppenderShouldAffectRootLogger() {
LogService.reconfigure();
assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();
final Logger rootLogger = (Logger) LogService.getRootLogger();
// assert "Console" is present for ROOT
Appender appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();
LogService.removeConsoleAppender();
// assert "Console" is not present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNull();
LogService.restoreConsoleAppender();
// assert "Console" is present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();
}
use of org.apache.logging.log4j.core.Appender in project ignite by apache.
the class Log4J2Logger method fileName.
/** {@inheritDoc} */
@Nullable
@Override
public String fileName() {
for (Logger log = impl; log != null; log = log.getParent()) {
for (Appender a : log.getAppenders().values()) {
if (a instanceof FileAppender)
return ((FileAppender) a).getFileName();
if (a instanceof RollingFileAppender)
return ((RollingFileAppender) a).getFileName();
if (a instanceof RoutingAppender) {
try {
RoutingAppender routing = (RoutingAppender) a;
Field appsFiled = routing.getClass().getDeclaredField("appenders");
appsFiled.setAccessible(true);
Map<String, AppenderControl> appenders = (Map<String, AppenderControl>) appsFiled.get(routing);
for (AppenderControl control : appenders.values()) {
Appender innerApp = control.getAppender();
if (innerApp instanceof FileAppender)
return normilize(((FileAppender) innerApp).getFileName());
if (innerApp instanceof RollingFileAppender)
return normilize(((RollingFileAppender) innerApp).getFileName());
}
} catch (IllegalAccessException | NoSuchFieldException e) {
error("Failed to get file name (was the implementation of log4j2 changed?).", e);
}
}
}
}
return null;
}
Aggregations