Search in sources :

Example 1 with StatusListener

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

the class CustomConfigurationTest method testConfig.

@Test
public void testConfig() {
    // don't bother using "error" since that's the default; try another level
    final LoggerContext ctx = this.init.getLoggerContext();
    ctx.reconfigure();
    final Configuration config = ctx.getConfiguration();
    assertThat(config, instanceOf(XmlConfiguration.class));
    for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
        if (listener instanceof StatusConsoleListener) {
            assertSame(listener.getStatusLevel(), Level.INFO);
            break;
        }
    }
    final Layout<? extends Serializable> layout = PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).withConfiguration(config).build();
    // @formatter:off
    final FileAppender appender = FileAppender.newBuilder().withFileName(LOG_FILE).withAppend(false).withName("File").withIgnoreExceptions(false).withBufferSize(4000).withBufferedIo(false).withLayout(layout).build();
    // @formatter:on
    appender.start();
    config.addAppender(appender);
    final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
    final AppenderRef[] refs = new AppenderRef[] { ref };
    final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j", "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("org.apache.logging.log4j", loggerConfig);
    ctx.updateLoggers();
    final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
    logger.info("This is a test");
    final File file = new File(LOG_FILE);
    assertThat(file, exists());
    assertThat(file, hasLength(greaterThan(0L)));
}
Also used : FileAppender(org.apache.logging.log4j.core.appender.FileAppender) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) StatusListener(org.apache.logging.log4j.status.StatusListener) File(java.io.File) Test(org.junit.Test)

Example 2 with StatusListener

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

the class HttpAppenderTest method testAppendErrorIgnore.

@Test
public void testAppendErrorIgnore() throws Exception {
    wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")).willReturn(FAILURE_RESPONSE));
    StatusLogger.getLogger().registerListener(new StatusListener() {

        @Override
        public void log(StatusData data) {
            error = data;
        }

        @Override
        public Level getStatusLevel() {
            return Level.ERROR;
        }

        @Override
        public void close() throws IOException {
        }
    });
    error = null;
    final Appender appender = HttpAppender.newBuilder().withName("Http").withLayout(JsonLayout.createDefaultLayout()).setConfiguration(ctx.getConfiguration()).setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")).build();
    appender.append(createLogEvent());
    wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")).withHeader("Host", containing("localhost")).withHeader("Content-Type", containing("application/json")).withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
    assertNotNull(error);
    assertEquals(Level.ERROR, error.getLevel());
    assertEquals("Unable to send HTTP in appender [Http]", error.getMessage().toString());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Level(org.apache.logging.log4j.Level) StatusListener(org.apache.logging.log4j.status.StatusListener) IOException(java.io.IOException) StatusData(org.apache.logging.log4j.status.StatusData) URL(java.net.URL) Test(org.junit.Test)

Example 3 with StatusListener

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

the class StatusConfiguration method configureExistingStatusConsoleListener.

private boolean configureExistingStatusConsoleListener() {
    boolean configured = false;
    for (final StatusListener statusListener : this.logger.getListeners()) {
        if (statusListener instanceof StatusConsoleListener) {
            final StatusConsoleListener listener = (StatusConsoleListener) statusListener;
            listener.setLevel(this.status);
            this.logger.updateListenerLevel(this.status);
            if (this.verbosity == Verbosity.QUIET) {
                listener.setFilters(this.verboseClasses);
            }
            configured = true;
        }
    }
    return configured;
}
Also used : StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) StatusListener(org.apache.logging.log4j.status.StatusListener)

Example 4 with StatusListener

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

the class StatusLoggerAdmin method removeListeners.

/**
     * Add listener to StatusLogger for this context, or replace it if it already exists.
     *
     * @param ctxName
     */
private void removeListeners(final String ctxName) {
    final StatusLogger logger = StatusLogger.getLogger();
    final Iterable<StatusListener> listeners = logger.getListeners();
    // Remove any StatusLoggerAdmin listeners already registered for this context
    for (final StatusListener statusListener : listeners) {
        if (statusListener instanceof StatusLoggerAdmin) {
            final StatusLoggerAdmin adminListener = (StatusLoggerAdmin) statusListener;
            if (ctxName != null && ctxName.equals(adminListener.contextName)) {
                logger.removeListener(adminListener);
            }
        }
    }
}
Also used : StatusLogger(org.apache.logging.log4j.status.StatusLogger) StatusListener(org.apache.logging.log4j.status.StatusListener)

Aggregations

StatusListener (org.apache.logging.log4j.status.StatusListener)4 StatusConsoleListener (org.apache.logging.log4j.status.StatusConsoleListener)2 StatusLogger (org.apache.logging.log4j.status.StatusLogger)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Level (org.apache.logging.log4j.Level)1 Logger (org.apache.logging.log4j.Logger)1 Appender (org.apache.logging.log4j.core.Appender)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)1 XmlConfiguration (org.apache.logging.log4j.core.config.xml.XmlConfiguration)1 StatusData (org.apache.logging.log4j.status.StatusData)1