@Override public Log4jTaglibLogger getLogger(final String name, final MessageFactory factory) { Log4jTaglibLogger logger = this.loggers.get(name); if (logger != null) { AbstractLogger.checkMessageFactory(logger, factory); return logger; } synchronized (this.loggers) { logger = this.loggers.get(name); if (logger == null) { final Logger original = factory == null ? LogManager.getLogger(name) : LogManager.getLogger(name, factory); if (!(original instanceof AbstractLogger)) { throw new LoggingException( "Log4j Tag Library requires base logging system to extend Log4j AbstractLogger." ); } // wrap a logger from an underlying implementation logger = new Log4jTaglibLogger((AbstractLogger) original, name, original.getMessageFactory()); this.loggers.put(name, logger); } } return logger; }
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."); }
@Test public void testGetLoggerWithTaglibLogger() throws Exception { this.setUp(null); final AbstractLogger wrapped = (AbstractLogger)LogManager.getLogger("testGetLoggerWithTaglibLogger"); final Log4jTaglibLogger logger = new Log4jTaglibLogger(wrapped, wrapped.getName(), wrapped.getMessageFactory()); this.tag.setLogger(logger); Log4jTaglibLogger returned = this.tag.getLogger(); assertNotNull("The first returned logger should not be null.", returned); assertSame("The first returned logger should be the same as the set.", logger, returned); assertEquals("The name is not correct.", "testGetLoggerWithTaglibLogger", returned.getName()); returned = this.tag.getLogger(); assertNotNull("The second returned logger should not be null.", returned); assertSame("The second returned logger should be the same as the set.", logger, returned); }
@Override public Logger getLogger(final String name) { final LoggerContext context = getContext(); final ConcurrentMap<String, Logger> loggers = getLoggersMap(context); if (loggers.containsKey(name)) { return loggers.get(name); } final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name; final org.apache.logging.log4j.Logger logger = context.getLogger(key); if (logger instanceof AbstractLogger) { loggers.putIfAbsent(name, new SLF4JLogger((AbstractLogger) logger, name)); return loggers.get(name); } throw new SLF4JLoggingException("SLF4J Adapter requires base logging system to extend Log4j AbstractLogger"); }
@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; }
/** * Actual writing occurs here. * * @param logEvent The LogEvent. */ @Override public void append(final LogEvent logEvent) { if (!isStarted()) { throw new IllegalStateException("AsyncAppender " + getName() + " is not active"); } final Log4jLogEvent memento = Log4jLogEvent.createMemento(logEvent, includeLocation); InternalAsyncUtil.makeMessageImmutable(logEvent.getMessage()); if (!transfer(memento)) { if (blocking) { if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031 // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock final Message message = AsyncQueueFullMessageUtil.transform(logEvent.getMessage()); logMessageInCurrentThread(new Log4jLogEvent.Builder(logEvent).setMessage(message).build()); } else { // delegate to the event router (which may discard, enqueue and block, or log in current thread) final EventRoute route = asyncQueueFullPolicy.getRoute(thread.getId(), memento.getLevel()); route.logMessage(this, memento); } } else { error("Appender " + getName() + " is unable to write primary appenders. queue is full"); logToErrorAppenderIfNecessary(false, memento); } } }
private void handleRingBufferFull(final RingBufferLogEventTranslator translator) { if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031 // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock final Message message = AsyncQueueFullMessageUtil.transform(translator.message); logMessageInCurrentThread(translator.fqcn, translator.level, translator.marker, message, translator.thrown); return; } final EventRoute eventRoute = loggerDisruptor.getEventRoute(translator.level); switch (eventRoute) { case ENQUEUE: loggerDisruptor.enqueueLogMessageInfo(translator); break; case SYNCHRONOUS: logMessageInCurrentThread(translator.fqcn, translator.level, translator.marker, translator.message, translator.thrown); break; case DISCARD: break; default: throw new IllegalStateException("Unknown EventRoute " + eventRoute); } }
private LogWriterLogger(final Logger logger, final String connectionName, final boolean isSecure) { super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory()); this.logWrapper = this; this.connectionName = connectionName; this.loggerName = getName(); this.isSecure = isSecure; }
public Log4j2Impl(Class<?> clazz) { Logger logger = LogManager.getLogger(clazz); if (logger instanceof AbstractLogger) { log = new Log4j2AbstractLoggerImpl((AbstractLogger) logger); } else { log = new Log4j2LoggerImpl(logger); } }
public Log4j2Impl(String clazz) { Logger logger = LogManager.getLogger(clazz); if (logger instanceof AbstractLogger) { log = new Log4j2AbstractLoggerImpl((AbstractLogger) logger); } else { log = new Log4j2LoggerImpl(logger); } }
public Log4j2Impl(String clazz) { Logger logger = LogManager.getLogger(clazz); if (logger instanceof AbstractLogger) { log = new Log4j2AbstractLoggerImpl((AbstractLogger)logger); } else { log = new Log4j2LoggerImpl(logger); } }
@Override public Logger getLogger(final String name, final MessageFactory messageFactory) { if (loggers.containsKey(name)) { final Logger logger = loggers.get(name); AbstractLogger.checkMessageFactory(logger, messageFactory); return logger; } loggers.putIfAbsent(name, new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime, showContextMap, dateTimeFormat, messageFactory, props, stream)); return loggers.get(name); }
@Override public Log getInstance(final String name) throws LogConfigurationException { final ConcurrentMap<String, Log> loggers = getLoggersMap(); if (loggers.containsKey(name)) { return loggers.get(name); } final org.apache.logging.log4j.Logger logger = PrivateManager.getLogger(name); if (logger instanceof AbstractLogger) { loggers.putIfAbsent(name, new Log4jLog((AbstractLogger) logger, name)); return loggers.get(name); } throw new LogConfigurationException( "Commons Logging Adapter requires base logging system to extend Log4j AbstractLogger"); }
/** * Obtain 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) { Logger logger = loggers.get(name); if (logger != null) { AbstractLogger.checkMessageFactory(logger, messageFactory); return logger; } logger = newInstance(this, name, messageFactory); final Logger prev = loggers.putIfAbsent(name, logger); return prev == null ? logger : prev; }
@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); }
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); } }
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); } }
/** * 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); }
private void handleQueueFull(final LogEvent event) { if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031 // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock final Message message = AsyncQueueFullMessageUtil.transform(event.getMessage()); callAppendersInCurrentThread(new Log4jLogEvent.Builder(event).setMessage(message).build()); } else { // otherwise, we leave it to the user preference final EventRoute eventRoute = delegate.getEventRoute(event.getLevel()); eventRoute.logMessage(this, event); } }
private void handleRingBufferFull(final StackTraceElement location, final String fqcn, final Level level, final Marker marker, final Message msg, final Throwable thrown) { if (AbstractLogger.getRecursionDepth() > 1) { // LOG4J2-1518, LOG4J2-2031 // If queue is full AND we are in a recursive call, call appender directly to prevent deadlock final Message message = AsyncQueueFullMessageUtil.transform(msg); logMessageInCurrentThread(fqcn, level, marker, message, thrown); return; } final EventRoute eventRoute = loggerDisruptor.getEventRoute(level); switch (eventRoute) { case ENQUEUE: loggerDisruptor.getDisruptor().getRingBuffer().publishEvent(this, this, // asyncLogger: 0 location, // location: 1 fqcn, // 2 level, // 3 marker, // 4 msg, // 5 thrown); // 6 break; case SYNCHRONOUS: logMessageInCurrentThread(fqcn, level, marker, msg, thrown); break; case DISCARD: break; default: throw new IllegalStateException("Unknown EventRoute " + eventRoute); } }
@Test public void testNoErrorIfLogAfterShutdown() throws Exception { final Logger log = LogManager.getLogger("com.foo.Bar"); log.info("some message"); CoreLoggerContexts.stopLoggerContext(); // stop async thread // call the #logMessage() method to bypass the isEnabled check: // before the LOG4J2-639 fix this would throw a NPE ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null); }
@Test public void testNoErrorIfLogAfterShutdown() throws Exception { final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg, new InternalError("this is not a real error")); CoreLoggerContexts.stopLoggerContext(); // stop async thread // call the #logMessage() method to bypass the isEnabled check: // before the LOG4J2-639 fix this would throw a NPE ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null); }
private CsapEventClientLogUnwind(final Logger logger) { super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory()); this.logger = this; }
public Log4j2AbstractLoggerImpl(AbstractLogger abstractLogger) { log = new ExtendedLoggerWrapper(abstractLogger, abstractLogger.getName(), abstractLogger.getMessageFactory()); }