Search in sources :

Example 1 with MessageFactory

use of org.apache.logging.log4j.message.MessageFactory in project logging-log4j2 by apache.

the class LoggerTest method checkMessageFactory.

private static void checkMessageFactory(final MessageFactory messageFactory1, final Logger testLogger1) {
    if (messageFactory1 == null) {
        assertEquals(AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, testLogger1.getMessageFactory().getClass());
    } else {
        MessageFactory actual = testLogger1.getMessageFactory();
        if (actual instanceof MessageFactory2Adapter) {
            actual = ((MessageFactory2Adapter) actual).getOriginal();
        }
        assertEquals(messageFactory1, actual);
    }
}
Also used : MessageFactory2Adapter(org.apache.logging.log4j.spi.MessageFactory2Adapter) ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) MessageFactory(org.apache.logging.log4j.message.MessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory)

Example 2 with MessageFactory

use of org.apache.logging.log4j.message.MessageFactory in project logging-log4j2 by apache.

the class SetLoggerTagTest method testDoEndTagStringFactoryVarPageScope.

@Test
public void testDoEndTagStringFactoryVarPageScope() throws Exception {
    this.tag.setLogger("testDoEndTagStringFactoryVarPageScope");
    final MessageFactory factory = new StringFormatterMessageFactory();
    this.tag.setFactory(factory);
    this.tag.setVar("goodbyeCruelWorld");
    assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context));
    assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
    assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context));
    final Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.PAGE_SCOPE);
    assertNotNull("The attribute should not be null.", attribute);
    assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger);
    final Log4jTaglibLogger logger = (Log4jTaglibLogger) attribute;
    assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryVarPageScope", logger.getName());
    checkMessageFactory("The message factory is not correct.", factory, logger);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) Test(org.junit.Test)

Example 3 with MessageFactory

use of org.apache.logging.log4j.message.MessageFactory in project logging-log4j2 by apache.

the class SetLoggerTagTest method testDoEndTagStringFactoryDefault.

@Test
public void testDoEndTagStringFactoryDefault() throws Exception {
    this.tag.setLogger("testDoEndTagStringFactoryDefault");
    final MessageFactory factory = new StringFormatterMessageFactory();
    this.tag.setFactory(factory);
    assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context));
    assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
    final Log4jTaglibLogger logger = TagUtils.getDefaultLogger(this.context);
    assertNotNull("The default logger should not be null anymore.", logger);
    assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryDefault", logger.getName());
    checkMessageFactory("The message factory is not correct.", factory, logger);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) Test(org.junit.Test)

Example 4 with MessageFactory

use of org.apache.logging.log4j.message.MessageFactory in project logging-log4j2 by apache.

the class SetLoggerTagTest method testDoEndTagStringFactoryVarApplicationScope.

@Test
public void testDoEndTagStringFactoryVarApplicationScope() throws Exception {
    this.tag.setLogger("testDoEndTagStringFactoryVarApplicationScope");
    final MessageFactory factory = new StringFormatterMessageFactory();
    this.tag.setFactory(factory);
    this.tag.setVar("goodbyeCruelWorld");
    this.tag.setScope("application");
    assertNull("The default logger should be null.", TagUtils.getDefaultLogger(this.context));
    assertEquals("The return value is not correct.", Tag.EVAL_PAGE, this.tag.doEndTag());
    assertNull("The default logger should still be null.", TagUtils.getDefaultLogger(this.context));
    final Object attribute = this.context.getAttribute("goodbyeCruelWorld", PageContext.APPLICATION_SCOPE);
    assertNotNull("The attribute should not be null.", attribute);
    assertTrue("The attribute should be a Log4jTaglibLogger.", attribute instanceof Log4jTaglibLogger);
    final Log4jTaglibLogger logger = (Log4jTaglibLogger) attribute;
    assertEquals("The logger name is not correct.", "testDoEndTagStringFactoryVarApplicationScope", logger.getName());
    checkMessageFactory("The message factory is not correct.", factory, logger);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory) Test(org.junit.Test)

Example 5 with MessageFactory

use of org.apache.logging.log4j.message.MessageFactory in project logging-log4j2 by apache.

the class ApiLogger method log.

@Override
public void log(final LogRecord record) {
    if (isFiltered(record)) {
        return;
    }
    final org.apache.logging.log4j.Level level = LevelTranslator.toLevel(record.getLevel());
    final Object[] parameters = record.getParameters();
    final MessageFactory messageFactory = logger.getMessageFactory();
    final Message message = parameters == null ? messageFactory.newMessage(record.getMessage()) : /* LOG4J2-1251: not formatted case */
    messageFactory.newMessage(record.getMessage(), parameters);
    final Throwable thrown = record.getThrown();
    logger.logIfEnabled(FQCN, level, null, message, thrown);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) Message(org.apache.logging.log4j.message.Message)

Aggregations

MessageFactory (org.apache.logging.log4j.message.MessageFactory)10 StringFormatterMessageFactory (org.apache.logging.log4j.message.StringFormatterMessageFactory)7 ParameterizedMessageFactory (org.apache.logging.log4j.message.ParameterizedMessageFactory)4 MessageFactory2Adapter (org.apache.logging.log4j.spi.MessageFactory2Adapter)4 Test (org.junit.Test)3 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)1 DefaultFlowMessageFactory (org.apache.logging.log4j.message.DefaultFlowMessageFactory)1 FlowMessageFactory (org.apache.logging.log4j.message.FlowMessageFactory)1 Message (org.apache.logging.log4j.message.Message)1 ReusableMessageFactory (org.apache.logging.log4j.message.ReusableMessageFactory)1 SimpleMessageFactory (org.apache.logging.log4j.message.SimpleMessageFactory)1 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)1