use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class ConcurrentLoggingWithJsonLayoutTest method testConcurrentLogging.
@Test
public void testConcurrentLogging() throws Throwable {
final Logger log = context.getLogger(ConcurrentLoggingWithJsonLayoutTest.class);
final Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
final List<Throwable> thrown = Collections.synchronizedList(new ArrayList<Throwable>());
for (int x = 0; x < Runtime.getRuntime().availableProcessors() * 2; x++) {
final Thread t = new LoggingThread(threads, log);
threads.add(t);
// Appender is configured with ignoreExceptions="false";
// any exceptions are propagated to the caller, so we can catch them here.
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
thrown.add(e);
}
});
t.start();
}
while (!threads.isEmpty()) {
log.info("not done going to sleep...");
Thread.sleep(10);
}
// if any error occurred, fail this test
if (!thrown.isEmpty()) {
throw thrown.get(0);
}
// simple test to ensure content is not corrupted
if (new File(PATH).exists()) {
final List<String> lines = Files.readAllLines(new File(PATH).toPath(), Charset.defaultCharset());
for (final String line : lines) {
assertThat(line, startsWith("{\"timeMillis\":"));
assertThat(line, endsWith("\"threadPriority\":5}"));
}
}
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class ConcurrentLoggingWithGelfLayoutTest method testConcurrentLogging.
@Test
public void testConcurrentLogging() throws Throwable {
final Logger log = context.getLogger(ConcurrentLoggingWithGelfLayoutTest.class);
final Set<Thread> threads = Collections.synchronizedSet(new HashSet<Thread>());
final List<Throwable> thrown = Collections.synchronizedList(new ArrayList<Throwable>());
for (int x = 0; x < Runtime.getRuntime().availableProcessors() * 2; x++) {
final Thread t = new LoggingThread(threads, log);
threads.add(t);
// Appender is configured with ignoreExceptions="false";
// any exceptions are propagated to the caller, so we can catch them here.
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
thrown.add(e);
}
});
t.start();
}
while (!threads.isEmpty()) {
log.info("not done going to sleep...");
Thread.sleep(10);
}
// if any error occurred, fail this test
if (!thrown.isEmpty()) {
throw thrown.get(0);
}
// simple test to ensure content is not corrupted
if (new File(PATH).exists()) {
final List<String> lines = Files.readAllLines(new File(PATH).toPath(), Charset.defaultCharset());
for (final String line : lines) {
assertThat(line, startsWith("{\"version\":\"1.1\",\"host\":\"myself\",\"timestamp\":"));
assertThat(line, endsWith("\"}"));
}
}
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class GelfLayoutTest2 method gelfLayout.
@Test
public void gelfLayout() throws IOException {
Logger logger = context.getLogger();
logger.info("Message");
String gelf = context.getListAppender("list").getMessages().get(0);
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(gelf);
assertEquals("Message", json.get("short_message").asText());
assertEquals("myhost", json.get("host").asText());
assertEquals("FOO", json.get("_foo").asText());
assertEquals(new JavaLookup().getRuntime(), json.get("_runtime").asText());
}
use of org.apache.logging.log4j.Logger in project logging-log4j2 by apache.
the class XIncludeTest method testLogger.
@Test
public void testLogger() throws Exception {
final Logger logger = this.ctx.getLogger(LOGGER_NAME);
assertThat(logger, is(instanceOf(org.apache.logging.log4j.core.Logger.class)));
final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
assertThat(l.getLevel(), is(equalTo(Level.DEBUG)));
assertThat(l.filterCount(), is(equalTo(1)));
final Iterator<Filter> iterator = l.getFilters();
assertThat(iterator.hasNext(), is(true));
final Filter filter = iterator.next();
assertThat(filter, is(instanceOf(ThreadContextMapFilter.class)));
final Map<String, Appender> appenders = l.getAppenders();
assertThat(appenders, is(notNullValue()));
assertThat(appenders, hasSize(1));
final Appender appender = appenders.get(APPENDER_NAME);
assertThat(appender, is(notNullValue()));
assertThat(appender.getName(), is(equalTo("STDOUT")));
}
use of org.apache.logging.log4j.Logger 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)));
}
Aggregations