Search in sources :

Example 1 with AbstractLogger

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

the class LoggerAwareTagSupportTest method testGetLoggerWithTaglibLogger.

@Test
public void testGetLoggerWithTaglibLogger() throws Exception {
    this.setUp(null);
    final AbstractLogger wrapped = (AbstractLogger) LogManager.getLogger("testGetLoggerWithTaglibLogger");
    final Log4jTaglibLogger logger = new Log4jTaglibLogger(wrapped, wrapped.getName(), wrapped.getMessageFactory());
    this.tag.setLogger(logger);
    Log4jTaglibLogger returned = this.tag.getLogger();
    assertNotNull("The first returned logger should not be null.", returned);
    assertSame("The first returned logger should be the same as the set.", logger, returned);
    assertEquals("The name is not correct.", "testGetLoggerWithTaglibLogger", returned.getName());
    returned = this.tag.getLogger();
    assertNotNull("The second returned logger should not be null.", returned);
    assertSame("The second returned logger should be the same as the set.", logger, returned);
}
Also used : AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 2 with AbstractLogger

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

the class TagUtils method resolveLogger.

static Log4jTaglibLogger resolveLogger(final Log4jTaglibLoggerContext context, final Object logger, final MessageFactory factory) throws JspException {
    if (logger instanceof Logger) {
        if (logger instanceof Log4jTaglibLogger) {
            return (Log4jTaglibLogger) logger;
        }
        if (logger instanceof AbstractLogger) {
            if (LOGGER.isInfoEnabled() && !WARNED_FOR.contains(logger)) {
                LOGGER.info("Constructing new Log4jTaglibLogger from AbstractLogger {} name and message factory.", logger.getClass().getName());
                WARNED_FOR.add(logger);
            }
            final AbstractLogger original = (AbstractLogger) logger;
            return getLogger(context, original.getName(), original.getMessageFactory());
        }
        throw new JspException("Log4j Tag Library requires base logging system to extend Log4j AbstractLogger.");
    }
    if (logger instanceof String) {
        return getLogger(context, (String) logger, factory);
    }
    throw new JspException("Logger must be of type String or org.apache.logging.log4j.Logger.");
}
Also used : JspException(javax.servlet.jsp.JspException) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger)

Example 3 with AbstractLogger

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

the class AsyncLoggerConfigUseAfterShutdownTest method testNoErrorIfLogAfterShutdown.

@Test
public void testNoErrorIfLogAfterShutdown() throws Exception {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    log.info("some message");
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    // call the #logMessage() method to bypass the isEnabled check:
    // before the LOG4J2-639 fix this would throw a NPE
    ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Example 4 with AbstractLogger

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

the class AsyncLoggerEventTranslationExceptionTest method testEventTranslationExceptionDoesNotCauseAsyncEventException.

@Test
void testEventTranslationExceptionDoesNotCauseAsyncEventException() {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    assertTrue(TestExceptionHandler.INSTANTIATED, "TestExceptionHandler was not configured properly");
    final Message exceptionThrowingMessage = new ExceptionThrowingMessage();
    assertThrows(TestMessageException.class, () -> ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, exceptionThrowingMessage, null));
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    assertFalse(TestExceptionHandler.EVENT_EXCEPTION_ENCOUNTERED, "ExceptionHandler encountered an event exception");
}
Also used : ReusableSimpleMessage(org.apache.logging.log4j.message.ReusableSimpleMessage) Message(org.apache.logging.log4j.message.Message) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.jupiter.api.Test)

Example 5 with AbstractLogger

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

the class AsyncLoggerUseAfterShutdownTest method testNoErrorIfLogAfterShutdown.

@Test
public void testNoErrorIfLogAfterShutdown() throws Exception {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    final String msg = "Async logger msg";
    log.info(msg, new InternalError("this is not a real error"));
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    // call the #logMessage() method to bypass the isEnabled check:
    // before the LOG4J2-639 fix this would throw a NPE
    ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.Test)

Aggregations

AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)5 Logger (org.apache.logging.log4j.Logger)4 Test (org.junit.Test)3 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 JspException (javax.servlet.jsp.JspException)1 Message (org.apache.logging.log4j.message.Message)1 ReusableSimpleMessage (org.apache.logging.log4j.message.ReusableSimpleMessage)1 StatusLogger (org.apache.logging.log4j.status.StatusLogger)1 Test (org.junit.jupiter.api.Test)1