org.apache.logging.log4j.message.MessageFactory Java Examples
The following examples show how to use
org.apache.logging.log4j.message.MessageFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Log4jTaglibLoggerContext.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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; }
Example #2
Source File: TagUtils.java From logging-log4j2 with Apache License 2.0 | 6 votes |
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."); }
Example #3
Source File: SetLoggerTagTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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); }
Example #4
Source File: SetLoggerTagTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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); }
Example #5
Source File: SetLoggerTagTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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); }
Example #6
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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; }
Example #7
Source File: StatusLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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); } }
Example #8
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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; }
Example #9
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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); } }
Example #10
Source File: Logger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * 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); }
Example #11
Source File: LoggerContext.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * 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); }
Example #12
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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); } }
Example #13
Source File: SLF4JLoggerContext.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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 }
Example #14
Source File: SetLoggerTagTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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); } }
Example #15
Source File: AbstractLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * 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); }
Example #16
Source File: AbstractLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * 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()); } }
Example #17
Source File: AbstractLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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; } }
Example #18
Source File: AbstractLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private static MessageFactory createDefaultMessageFactory() { try { return DEFAULT_MESSAGE_FACTORY_CLASS.newInstance(); } catch (final InstantiationException | IllegalAccessException e) { throw new IllegalStateException(e); } }
Example #19
Source File: ApiLogger.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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); }
Example #20
Source File: SimpleLoggerContext.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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); }
Example #21
Source File: TagUtils.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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); } }
Example #22
Source File: Log4jTaglibLogger.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public Log4jTaglibLogger(final ExtendedLogger logger, final String name, final MessageFactory messageFactory) { super(logger, name, messageFactory); }
Example #23
Source File: Log4jTaglibLoggerContext.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public boolean hasLogger(final String name, final MessageFactory messageFactory) { return loggerRegistry.hasLogger(name, messageFactory); }
Example #24
Source File: SLF4JLogger.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) { super(name, messageFactory); this.logger = logger; this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null; }
Example #25
Source File: TestLoggerContext.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { return new TestLogger(name, messageFactory); }
Example #26
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private void assertEqualMessageFactory(final MessageFactory messageFactory, final TestLogger testLogger) { MessageFactory actual = testLogger.getMessageFactory(); assertEquals(messageFactory, actual); }
Example #27
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private static void assertMessageFactoryInstanceOf(MessageFactory factory, final Class<?> cls) { assertTrue(factory.getClass().isAssignableFrom(cls)); }
Example #28
Source File: TestLogger.java From logging-log4j2 with Apache License 2.0 | 4 votes |
public TestLogger(final String name, final MessageFactory messageFactory) { super(name, messageFactory); }
Example #29
Source File: TestLoggerContext.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) { return false; }
Example #30
Source File: AsyncLoggerContext.java From logging-log4j2 with Apache License 2.0 | 4 votes |
@Override protected Logger newInstance(final LoggerContext ctx, final String name, final MessageFactory messageFactory) { return new AsyncLogger(ctx, name, messageFactory, loggerDisruptor); }