Search in sources :

Example 1 with ExtendedLogger

use of org.apache.logging.log4j.spi.ExtendedLogger in project logging-log4j2 by apache.

the class TestLoggerContext method getLogger.

@Override
public ExtendedLogger getLogger(final String name) {
    final ExtendedLogger extendedLogger = map.get(name);
    if (extendedLogger != null) {
        return extendedLogger;
    }
    final ExtendedLogger logger = new TestLogger(name);
    map.put(name, logger);
    return logger;
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger)

Example 2 with ExtendedLogger

use of org.apache.logging.log4j.spi.ExtendedLogger in project logging-log4j2 by apache.

the class Category method maybeLog.

void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level, final Object message, final Throwable throwable) {
    if (logger.isEnabled(level)) {
        final Message msg;
        if (message instanceof String) {
            msg = new SimpleMessage((String) message);
        } else // SimpleMessage treats String and CharSequence differently, hence this else-if block.
        if (message instanceof CharSequence) {
            msg = new SimpleMessage((CharSequence) message);
        } else if (message instanceof Map) {
            @SuppressWarnings("unchecked") final Map<String, ?> map = (Map<String, ?>) message;
            msg = new MapMessage<>(map);
        } else {
            msg = new ObjectMessage(message);
        }
        if (logger instanceof ExtendedLogger) {
            ((ExtendedLogger) logger).logMessage(fqcn, level, null, msg, throwable);
        } else {
            logger.log(level, msg, throwable);
        }
    }
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) LocalizedMessage(org.apache.logging.log4j.message.LocalizedMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) RendererMap(org.apache.log4j.or.RendererMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 3 with ExtendedLogger

use of org.apache.logging.log4j.spi.ExtendedLogger in project logging-log4j2 by apache.

the class Log4j2Jira1688Test method testLog4j2Only.

@Test
public void testLog4j2Only() throws InterruptedException {
    final org.apache.logging.log4j.Logger log4JLogger = LogManager.getLogger(this.getClass());
    // more than unrolled varargs
    final int limit = 11;
    final Object[] args = createArray(limit);
    final Object[] originalArgs = Arrays.copyOf(args, args.length);
    listAppender.countDownLatch = new CountDownLatch(1);
    ((ExtendedLogger) log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);
    listAppender.countDownLatch.await(1, TimeUnit.SECONDS);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);
    ((ExtendedLogger) log4JLogger).logIfEnabled("test", Level.ERROR, null, "test {}", args);
    Assert.assertArrayEquals(Arrays.toString(args), originalArgs, args);
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ExtendedLogger

use of org.apache.logging.log4j.spi.ExtendedLogger in project logging-log4j2 by apache.

the class AsyncAppenderTest method testNoLocationInformation.

@Test
@LoggerContextSource("log4j-asynch-no-location.xml")
public void testNoLocationInformation(final LoggerContext context, @Named("List") final ListAppender appender) throws InterruptedException {
    final ExtendedLogger logger = context.getLogger(getClass());
    logger.error("This is a test");
    logger.warn("Hello world!");
    final List<String> messages;
    try {
        messages = appender.getMessages(2, 2, TimeUnit.SECONDS);
    } finally {
        appender.clear();
    }
    assertNotNull(messages);
    assertEquals(2, messages.size());
    assertEquals("?  This is a test", messages.get(0));
    assertEquals("?  Hello world!", messages.get(1));
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) Test(org.junit.jupiter.api.Test) LoggerContextSource(org.apache.logging.log4j.core.test.junit.LoggerContextSource)

Example 5 with ExtendedLogger

use of org.apache.logging.log4j.spi.ExtendedLogger in project pinpoint by naver.

the class Log4j2Binder method getLogger.

@Override
public PLogger getLogger(String name) {
    final PLogger hitPLogger = loggerCache.get(name);
    if (hitPLogger != null) {
        return hitPLogger;
    }
    ExtendedLogger logger = loggerContext.getLogger(name);
    PLogger log4j2Adapter = new Log4j2PLoggerAdapter(logger);
    final PLogger before = loggerCache.putIfAbsent(name, log4j2Adapter);
    if (before != null) {
        return before;
    }
    return log4j2Adapter;
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) PLogger(com.navercorp.pinpoint.bootstrap.logging.PLogger)

Aggregations

ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)15 ListAppender (org.apache.logging.log4j.core.test.appender.ListAppender)3 Test (org.junit.jupiter.api.Test)3 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 RendererMap (org.apache.log4j.or.RendererMap)2 LoggerContextSource (org.apache.logging.log4j.core.test.junit.LoggerContextSource)2 LocalizedMessage (org.apache.logging.log4j.message.LocalizedMessage)2 MapMessage (org.apache.logging.log4j.message.MapMessage)2 Message (org.apache.logging.log4j.message.Message)2 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 TestLogger (org.apache.logging.log4j.test.TestLogger)2 Test (org.junit.Test)2 PLogger (com.navercorp.pinpoint.bootstrap.logging.PLogger)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1