Search in sources :

Example 6 with Appender

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);
}
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 7 with Appender

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();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderContext(org.apache.geode.internal.logging.log4j.AppenderContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 8 with Appender

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();
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) AppenderContext(org.apache.geode.internal.logging.log4j.AppenderContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 9 with Appender

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Logger(org.apache.logging.log4j.core.Logger) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with Appender

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;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) AppenderControl(org.apache.logging.log4j.core.config.AppenderControl) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) IgniteLogger(org.apache.ignite.IgniteLogger) Logger(org.apache.logging.log4j.core.Logger) Field(java.lang.reflect.Field) RoutingAppender(org.apache.logging.log4j.core.appender.routing.RoutingAppender) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)98 Test (org.junit.Test)50 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerContext (org.apache.logging.log4j.core.LoggerContext)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)13 Logger (org.apache.logging.log4j.Logger)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)11 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)8 URL (java.net.URL)7 IOException (java.io.IOException)5 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Logger (org.apache.logging.log4j.core.Logger)5 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)5