Search in sources :

Example 51 with Logger

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}"));
        }
    }
}
Also used : Logger(org.apache.logging.log4j.Logger) File(java.io.File) Test(org.junit.Test)

Example 52 with Logger

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("\"}"));
        }
    }
}
Also used : Logger(org.apache.logging.log4j.Logger) File(java.io.File) Test(org.junit.Test)

Example 53 with Logger

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());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Logger(org.apache.logging.log4j.Logger) JavaLookup(org.apache.logging.log4j.core.lookup.JavaLookup) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 54 with Logger

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")));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Filter(org.apache.logging.log4j.core.Filter) ThreadContextMapFilter(org.apache.logging.log4j.core.filter.ThreadContextMapFilter) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Example 55 with Logger

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

Aggregations

Logger (org.apache.logging.log4j.Logger)491 Test (org.junit.Test)226 File (java.io.File)80 Test (org.junit.jupiter.api.Test)69 IOException (java.io.IOException)34 LoggerContext (org.apache.logging.log4j.core.LoggerContext)33 Appender (org.apache.logging.log4j.core.Appender)32 Collectors (java.util.stream.Collectors)30 StatusLogger (org.apache.logging.log4j.status.StatusLogger)30 BufferedReader (java.io.BufferedReader)29 Level (org.apache.logging.log4j.Level)27 FileReader (java.io.FileReader)26 Path (java.nio.file.Path)26 CountDownLatch (java.util.concurrent.CountDownLatch)23 Map (java.util.Map)21 IntStream (java.util.stream.IntStream)20 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)20 java.util (java.util)18 HashMap (java.util.HashMap)18 Configuration (org.apache.logging.log4j.core.config.Configuration)18