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);
}
}
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));
}
}
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");
}
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();
}
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("[1;35mFatal message.[0m");
LOG.error("[1;31mError message.[0m");
LOG.warn("[0;33mWarning message.[0m");
LOG.info("[0;32mInformation message.[0m");
LOG.debug("[0;36mDebug message.[0m");
LOG.trace("[0;30mTrace message.[0m");
LOG.error("[1;31mError message.[0m", new IOException("test"));
}
}
Aggregations