Search in sources :

Example 46 with LoggerContext

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

the class ConsoleAppenderJAnsiXExceptionMain method test.

public void test(final String[] args) {
    // System.out.println(System.getProperty("java.class.path"));
    final String config = args == null || args.length == 0 ? "target/test-classes/log4j2-console-xex-ansi.xml" : args[0];
    final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config);
    final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiXExceptionMain.class);
    try {
        Files.getFileStore(Paths.get("?BOGUS?"));
    } catch (final Exception e) {
        final IllegalArgumentException logE = new IllegalArgumentException("Bad argument foo", e);
        logger.error("Gotcha!", logE);
    } finally {
        Configurator.shutdown(ctx);
    }
}
Also used : Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 47 with LoggerContext

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

the class ConsoleAppenderNoAnsiStyleLayoutMain method test.

static void test(final String[] args, final String config) {
    // System.out.println(System.getProperty("java.class.path"));
    try (final LoggerContext ctx = Configurator.initialize(ConsoleAppenderNoAnsiStyleLayoutMain.class.getName(), config)) {
        LOG.fatal("Fatal message.");
        LOG.error("Error message.");
        LOG.warn("Warning message.");
        LOG.info("Information message.");
        LOG.debug("Debug message.");
        LOG.trace("Trace message.");
        logThrowableFromMethod();
        // This will log the stack trace as well:
        final IOException ioException = new IOException("test");
        LOG.error("Error message {}", "Hi", ioException);
        final Throwable t = new IOException("test suppressed");
        t.addSuppressed(new IOException("test suppressed 2", ioException));
        LOG.error("Error message {}, suppressed?", "Hi", t);
        LOG.error("Error message {}, suppressed?", "Hi", new IOException("test", t));
    }
}
Also used : IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 48 with LoggerContext

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

the class OutputStreamAppenderTest method testUpdatePatternWithFileAppender.

/**
     * Validates that the code pattern we use to add an appender on the fly
     * works with a basic appender that is not the new OutputStream appender or
     * new Writer appender.
     */
@Test
public void testUpdatePatternWithFileAppender() {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    // @formatter:off
    final Appender appender = FileAppender.newBuilder().withFileName("target/" + getClass().getName() + ".log").withAppend(false).withName("File").withIgnoreExceptions(false).withBufferedIo(false).withBufferSize(4000).setConfiguration(config).build();
    // @formatter:on
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
    LogManager.getLogger().error("FOO MSG");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 49 with LoggerContext

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

the class AsyncAppenderShutdownTimeoutTest method shutdownTest.

@Test(timeout = 5000)
public void shutdownTest() throws Exception {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Logger logger = ctx.getLogger("Logger");
    logger.info("This is a test");
    ctx.stop();
}
Also used : Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Example 50 with LoggerContext

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

the class ConsoleAppenderAnsiMessagesMain method main.

public static void main(final String[] args) {
    try (final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), "target/test-classes/log4j2-console.xml")) {
        LOG.fatal("Fatal message.");
        LOG.error("Error message.");
        LOG.warn("Warning message.");
        LOG.info("Information message.");
        LOG.debug("Debug message.");
        LOG.trace("Trace message.");
        LOG.error("Error message.", new IOException("test"));
    }
}
Also used : IOException(java.io.IOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4