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