类org.apache.logging.log4j.message.MessageFactory源码实例Demo

下面列出了怎么用org.apache.logging.log4j.message.MessageFactory的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: logging-log4j2   文件: Log4jTaglibLoggerContext.java
@Override
public Log4jTaglibLogger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    Log4jTaglibLogger logger = this.loggerRegistry.getLogger(name, messageFactory);
    if (logger != null) {
        AbstractLogger.checkMessageFactory(logger, messageFactory);
        return logger;
    }

    synchronized (this.loggerRegistry) {
        logger = this.loggerRegistry.getLogger(name, messageFactory);
        if (logger == null) {
            final LoggerContext context = LogManager.getContext(false);
            final ExtendedLogger original = messageFactory == null ?
                    context.getLogger(name) : context.getLogger(name, messageFactory);
            // wrap a logger from an underlying implementation
            logger = new Log4jTaglibLogger(original, name, original.getMessageFactory());
            this.loggerRegistry.putIfAbsent(name, messageFactory, logger);
        }
    }

    return logger;
}
 
源代码2 项目: logging-log4j2   文件: TagUtils.java
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.");
}
 
源代码3 项目: logging-log4j2   文件: SetLoggerTagTest.java
@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);
}
 
源代码4 项目: logging-log4j2   文件: SetLoggerTagTest.java
@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);
}
 
源代码5 项目: logging-log4j2   文件: SetLoggerTagTest.java
@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);
}
 
源代码6 项目: logging-log4j2   文件: StatusLogger.java
private StatusLogger(final String name, final MessageFactory messageFactory) {
    super(name, messageFactory);
    final String dateFormat = PROPS.getStringProperty(STATUS_DATE_FORMAT, Strings.EMPTY);
    final boolean showDateTime = !Strings.isEmpty(dateFormat);
    this.logger = new SimpleLogger("StatusLogger", Level.ERROR, false, true, showDateTime, false,
            dateFormat, messageFactory, PROPS, System.err);
    this.listenersLevel = Level.toLevel(DEFAULT_STATUS_LEVEL, Level.WARN).intLevel();

    // LOG4J2-1813 if system property "log4j2.debug" is defined, print all status logging
    if (isDebugPropertyEnabled()) {
        logger.setLevel(Level.TRACE);
    }
}
 
源代码7 项目: logging-log4j2   文件: AbstractLogger.java
/**
 * Creates a new named logger with a particular {@link MessageFactory}.
 *
 * @param name the logger name
 * @param messageFactory the message factory, if null then use the default message factory.
 */
public AbstractLogger(final String name, final MessageFactory messageFactory) {
    this.name = name;
    this.messageFactory = messageFactory == null ? createDefaultMessageFactory() : messageFactory;
    this.flowMessageFactory = createDefaultFlowMessageFactory();
    this.logBuilder = new LocalLogBuilder(this);
}
 
源代码8 项目: logging-log4j2   文件: AbstractLogger.java
/**
 * 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());
    }
}
 
源代码9 项目: logging-log4j2   文件: AbstractLogger.java
private static Class<? extends MessageFactory> createClassForProperty(final String property,
        final Class<ReusableMessageFactory> reusableParameterizedMessageFactoryClass,
        final Class<ParameterizedMessageFactory> parameterizedMessageFactoryClass) {
    try {
        final String fallback = Constants.ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName()
                : parameterizedMessageFactoryClass.getName();
        final String clsName = PropertiesUtil.getProperties().getStringProperty(property, fallback);
        return LoaderUtil.loadClass(clsName).asSubclass(MessageFactory.class);
    } catch (final Throwable t) {
        return parameterizedMessageFactoryClass;
    }
}
 
源代码10 项目: logging-log4j2   文件: AbstractLogger.java
private static MessageFactory createDefaultMessageFactory() {
    try {
        return DEFAULT_MESSAGE_FACTORY_CLASS.newInstance();
    } catch (final InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}
 
源代码11 项目: logging-log4j2   文件: SimpleLoggerContext.java
@Override
public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
    if (extendedLogger != null) {
        AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
        return extendedLogger;
    }
    final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
            showContextMap, dateTimeFormat, messageFactory, props, stream);
    loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger);
    return loggerRegistry.getLogger(name, messageFactory);
}
 
源代码12 项目: logging-log4j2   文件: TagUtils.java
private static Log4jTaglibLogger getLogger(final Log4jTaglibLoggerContext context, final String name,
                                           final MessageFactory factory)
        throws JspException {
    try {
        return context.getLogger(name, factory);
    } catch (final LoggingException e) {
        throw new JspException(e.getMessage(), e);
    }
}
 
源代码13 项目: logging-log4j2   文件: SetLoggerTagTest.java
private static void checkMessageFactory(final String msg, final MessageFactory messageFactory,
        final Logger testLogger) {
    if (messageFactory == null) {
        assertEquals(msg, AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS,
                testLogger.getMessageFactory().getClass());
    } else {
        MessageFactory actual = testLogger.getMessageFactory();
        assertEquals(msg, messageFactory, actual);
    }
}
 
源代码14 项目: logging-log4j2   文件: ApiLogger.java
@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);
}
 
源代码15 项目: logging-log4j2   文件: SLF4JLoggerContext.java
@Override
public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
    // FIXME according to LOG4J2-1180, the below line should be:
    // FIXME if (!loggerRegistry.hasLogger(name, messageFactory)) {
    if (!loggerRegistry.hasLogger(name)) {
        // FIXME: should be loggerRegistry.putIfAbsent(name, messageFactory,
        loggerRegistry.putIfAbsent(name, null,
                new SLF4JLogger(name, messageFactory, LoggerFactory.getLogger(name)));
    }
    // FIXME should be return loggerRegistry.getLogger(name, messageFactory);
    return loggerRegistry.getLogger(name);

    // TODO applying the above fixes causes (log4j-to-slf4j) LoggerTest to fail
}
 
源代码16 项目: logging-log4j2   文件: LoggerTest.java
private Logger testMessageFactoryMismatch(final String name, final MessageFactory messageFactory1, final MessageFactory messageFactory2) {
    final Logger testLogger = LogManager.getLogger(name, messageFactory1);
    assertThat(testLogger, is(notNullValue()));
    checkMessageFactory(messageFactory1, testLogger);
    final Logger testLogger2 = LogManager.getLogger(name, messageFactory2);
    checkMessageFactory(messageFactory1, testLogger2);
    return testLogger;
}
 
源代码17 项目: logging-log4j2   文件: LoggerTest.java
private static void checkMessageFactory(final MessageFactory messageFactory, final Logger testLogger) {
    if (messageFactory == null) {
        assertEquals(AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, testLogger.getMessageFactory().getClass());
    } else {
        MessageFactory actual = testLogger.getMessageFactory();
        assertEquals(messageFactory, actual);
    }
}
 
源代码18 项目: logging-log4j2   文件: Logger.java
/**
 * 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);
}
 
源代码19 项目: logging-log4j2   文件: LoggerContext.java
/**
 * Obtains a Logger from the Context.
 *
 * @param name The name of the Logger to return.
 * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the
 *            logger but will log a warning if mismatched.
 * @return The Logger.
 */
@Override
public Logger getLogger(final String name, final MessageFactory messageFactory) {
    // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
    Logger logger = loggerRegistry.getLogger(name, messageFactory);
    if (logger != null) {
        AbstractLogger.checkMessageFactory(logger, messageFactory);
        return logger;
    }

    logger = newInstance(this, name, messageFactory);
    loggerRegistry.putIfAbsent(name, messageFactory, logger);
    return loggerRegistry.getLogger(name, messageFactory);
}
 
源代码20 项目: logging-log4j2   文件: LoggerTest.java
private static Logger testMessageFactoryMismatch(final String name,
                                                 final MessageFactory messageFactory1,
        final MessageFactory messageFactory2) {
    final Logger testLogger1 = (Logger) LogManager.getLogger(name, messageFactory1);
    assertNotNull(testLogger1);
    checkMessageFactory(messageFactory1, testLogger1);
    final Logger testLogger2 = (Logger) LogManager.getLogger(name, messageFactory2);
    assertNotNull(testLogger2);
    checkMessageFactory(messageFactory2, testLogger2);
    return testLogger1;
}
 
源代码21 项目: logging-log4j2   文件: LoggerTest.java
private static void checkMessageFactory(final MessageFactory messageFactory, final Logger testLogger) {
    if (messageFactory == null) {
        assertEquals(AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, testLogger.getMessageFactory().getClass());
    } else {
        MessageFactory actual = testLogger.getMessageFactory();
        assertEquals(messageFactory, actual);
    }
}
 
源代码22 项目: bartworks   文件: STFUGTPPLOG.java
@Override
public MessageFactory getMessageFactory() {
    return null;
}
 
源代码23 项目: core-ng-project   文件: ESLoggerContext.java
@Override
public ExtendedLogger getLogger(String name, MessageFactory messageFactory) {
    return loggers.computeIfAbsent(name, key -> new ESLogger(key, messageFactory, (LoggerImpl) LoggerFactory.getLogger(key)));
}
 
源代码24 项目: core-ng-project   文件: ESLoggerContext.java
@Override
public boolean hasLogger(String name, MessageFactory messageFactory) {
    return hasLogger(name);
}
 
源代码25 项目: core-ng-project   文件: ESLoggerContext.java
@Override
public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) {
    return hasLogger(name);
}
 
源代码26 项目: core-ng-project   文件: ESLogger.java
public ESLogger(String name, MessageFactory messageFactory, LoggerImpl logger) {
    super(name, messageFactory);
    this.logger = logger;
}
 
源代码27 项目: logging-log4j2   文件: LoggerRegistry.java
private static String factoryClassKey(final Class<? extends MessageFactory> messageFactoryClass) {
    return messageFactoryClass == null ? DEFAULT_FACTORY_KEY : messageFactoryClass.getName();
}
 
源代码28 项目: logging-log4j2   文件: LoggerRegistry.java
private static String factoryKey(final MessageFactory messageFactory) {
    return messageFactory == null ? DEFAULT_FACTORY_KEY : messageFactory.getClass().getName();
}
 
源代码29 项目: logging-log4j2   文件: LoggerRegistry.java
public void putIfAbsent(final String name, final MessageFactory messageFactory, final T logger) {
    factory.putIfAbsent(getOrCreateInnerMap(factoryKey(messageFactory)), name, logger);
}
 
源代码30 项目: logging-log4j2   文件: AbstractLogger.java
@SuppressWarnings("unchecked")
@Override
public <MF extends MessageFactory> MF getMessageFactory() {
    return (MF) messageFactory;
}
 
 类所在包
 类方法
 同包方法