Search in sources :

Example 6 with MessageFactory

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

the class Logger method getParent.

/**
     * This method is only used for 1.x compatibility. Returns the parent of this Logger. If it doesn't already exist
     * return a temporary Logger.
     *
     * @return The parent Logger.
     */
public Logger getParent() {
    final LoggerConfig lc = privateConfig.loggerConfig.getName().equals(getName()) ? privateConfig.loggerConfig.getParent() : privateConfig.loggerConfig;
    if (lc == null) {
        return null;
    }
    final String lcName = lc.getName();
    final MessageFactory messageFactory = getMessageFactory();
    if (context.hasLogger(lcName, messageFactory)) {
        return context.getLogger(lcName, messageFactory);
    }
    return new Logger(context, lcName, messageFactory);
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 7 with MessageFactory

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

the class LoggerTest method assertEqualMessageFactory.

private void assertEqualMessageFactory(final MessageFactory messageFactory, final TestLogger testLogger) {
    MessageFactory actual = testLogger.getMessageFactory();
    if (actual instanceof MessageFactory2Adapter) {
        actual = ((MessageFactory2Adapter) actual).getOriginal();
    }
    assertEquals(messageFactory, actual);
}
Also used : MessageFactory2Adapter(org.apache.logging.log4j.spi.MessageFactory2Adapter) MessageFactory(org.apache.logging.log4j.message.MessageFactory) ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) SimpleMessageFactory(org.apache.logging.log4j.message.SimpleMessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory)

Example 8 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) MessageFactory(org.apache.logging.log4j.message.MessageFactory) ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) StringFormatterMessageFactory(org.apache.logging.log4j.message.StringFormatterMessageFactory)

Example 9 with MessageFactory

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

the class SetLoggerTagTest method checkMessageFactory.

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

Example 10 with MessageFactory

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

the class AbstractLogger method checkMessageFactory.

/**
     * Checks that the message factory a logger was created with is the same as the given messageFactory. If they are
     * different log a warning to the {@linkplain StatusLogger}. A null MessageFactory translates to the default
     * MessageFactory {@link #DEFAULT_MESSAGE_FACTORY_CLASS}.
     *
     * @param logger The logger to check
     * @param messageFactory The message factory to check.
     */
public static void checkMessageFactory(final ExtendedLogger logger, final MessageFactory messageFactory) {
    final String name = logger.getName();
    final MessageFactory loggerMessageFactory = logger.getMessageFactory();
    if (messageFactory != null && !loggerMessageFactory.equals(messageFactory)) {
        StatusLogger.getLogger().warn("The Logger {} was created with the message factory {} and is now requested with the " + "message factory {}, which may create log events with unexpected formatting.", name, loggerMessageFactory, messageFactory);
    } else if (messageFactory == null && !loggerMessageFactory.getClass().equals(DEFAULT_MESSAGE_FACTORY_CLASS)) {
        StatusLogger.getLogger().warn("The Logger {} was created with the message factory {} and is now requested with a null " + "message factory (defaults to {}), which may create log events with unexpected " + "formatting.", name, loggerMessageFactory, DEFAULT_MESSAGE_FACTORY_CLASS.getName());
    }
}
Also used : MessageFactory(org.apache.logging.log4j.message.MessageFactory) ParameterizedMessageFactory(org.apache.logging.log4j.message.ParameterizedMessageFactory) FlowMessageFactory(org.apache.logging.log4j.message.FlowMessageFactory) DefaultFlowMessageFactory(org.apache.logging.log4j.message.DefaultFlowMessageFactory) ReusableMessageFactory(org.apache.logging.log4j.message.ReusableMessageFactory)

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