org.apache.logging.log4j.spi.LoggerContext Java Examples

The following examples show how to use org.apache.logging.log4j.spi.LoggerContext. 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: Category.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public ResourceBundle getResourceBundle() {
    if (bundle != null) {
        return bundle;
    }
    String name = logger.getName();
    if (isCoreAvailable) {
        LoggerContext ctx = CategoryUtil.getLoggerContext(logger);
        if (ctx != null) {
            final ConcurrentMap<String, Logger> loggers = getLoggersMap(ctx);
            while ((name = getSubName(name)) != null) {
                final Logger subLogger = loggers.get(name);
                if (subLogger != null) {
                    final ResourceBundle rb = subLogger.bundle;
                    if (rb != null) {
                        return rb;
                    }
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: Category.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public ResourceBundle getResourceBundle() {
    if (bundle != null) {
        return bundle;
    }
    String name = logger.getName();
    if (isCoreAvailable) {
        LoggerContext ctx = CategoryUtil.getLoggerContext(logger);
        if (ctx != null) {
            final ConcurrentMap<String, Logger> loggers = getLoggersMap(ctx);
            while ((name = getSubName(name)) != null) {
                final Logger subLogger = loggers.get(name);
                if (subLogger != null) {
                    final ResourceBundle rb = subLogger.bundle;
                    if (rb != null) {
                        return rb;
                    }
                }
            }
        }
    }
    return null;
}
 
Example #3
Source File: Log4jTaglibLoggerContext.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: AutoConfigTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testListAppender() {
    Logger logger = LogManager.getLogger("test");
    logger.debug("This is a test of the root logger");
    LoggerContext loggerContext = org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = ((org.apache.logging.log4j.core.LoggerContext) loggerContext).getConfiguration();
    Map<String, Appender> appenders = configuration.getAppenders();
    ListAppender eventAppender = null;
    ListAppender messageAppender = null;
    for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("list")) {
            messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        } else if (entry.getKey().equals("events")) {
            eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        }
    }
    assertNotNull("No Event Appender", eventAppender);
    assertNotNull("No Message Appender", messageAppender);
    List<LoggingEvent> events = eventAppender.getEvents();
    assertTrue("No events", events != null && events.size() > 0);
    List<String> messages = messageAppender.getMessages();
    assertTrue("No messages", messages != null && messages.size() > 0);
}
 
Example #5
Source File: XmlConfigurationFactoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testListAppender() {
    XmlConfigurationFactory.configure("target/test-classes/log4j1-list.xml");
    Logger logger = LogManager.getLogger("test");
    logger.debug("This is a test of the root logger");
    LoggerContext loggerContext = org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = ((org.apache.logging.log4j.core.LoggerContext) loggerContext).getConfiguration();
    Map<String, Appender> appenders = configuration.getAppenders();
    ListAppender eventAppender = null;
    ListAppender messageAppender = null;
    for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("list")) {
            messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        } else if (entry.getKey().equals("events")) {
            eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        }
    }
    assertNotNull("No Event Appender", eventAppender);
    assertNotNull("No Message Appender", messageAppender);
    List<LoggingEvent> events = eventAppender.getEvents();
    assertTrue("No events", events != null && events.size() > 0);
    List<String> messages = messageAppender.getMessages();
    assertTrue("No messages", messages != null && messages.size() > 0);
}
 
Example #6
Source File: AutoConfigTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testListAppender() {
    Logger logger = LogManager.getLogger("test");
    logger.debug("This is a test of the root logger");
    LoggerContext loggerContext = org.apache.logging.log4j.LogManager.getContext(false);
    Configuration configuration = ((org.apache.logging.log4j.core.LoggerContext) loggerContext).getConfiguration();
    Map<String, Appender> appenders = configuration.getAppenders();
    ListAppender eventAppender = null;
    ListAppender messageAppender = null;
    for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
        if (entry.getKey().equals("list")) {
            messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        } else if (entry.getKey().equals("events")) {
            eventAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
        }
    }
    assertNotNull("No Event Appender", eventAppender);
    assertNotNull("No Message Appender", messageAppender);
    List<LoggingEvent> events = eventAppender.getEvents();
    assertTrue("No events", events != null && events.size() > 0);
    List<String> messages = messageAppender.getMessages();
    assertTrue("No messages", messages != null && messages.size() > 0);
}
 
Example #7
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
static Logger getInstance(final LoggerContext context, final String name, final PrivateAdapter factory) {
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(context);
    Logger logger = loggers.get(name);
    if (logger != null) {
        return logger;
    }
    logger = factory.newLogger(name, context);
    final Logger prev = loggers.putIfAbsent(name, logger);
    return prev == null ? logger : prev;
}
 
Example #8
Source File: Logging.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String getContextName(LoggerContext context) {
    if(context instanceof org.apache.logging.log4j.core.LoggerContext) {
        return ((org.apache.logging.log4j.core.LoggerContext) context).getName();
    } else {
        return context.getClass().getSimpleName();
    }
}
 
Example #9
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a LoggerContext.
 *
 * @param currentContext if false the LoggerContext appropriate for the caller of this method is returned. For
 *            example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
 *            returned and if the caller is a class in the container's classpath then a different LoggerContext may
 *            be returned. If true then only a single LoggerContext will be returned.
 * @return a LoggerContext.
 */
public static LoggerContext getContext(final boolean currentContext) {
    // TODO: would it be a terrible idea to try and find the caller ClassLoader here?
    try {
        return factory.getContext(FQCN, null, null, currentContext, null, null);
    } catch (final IllegalStateException ex) {
        LOGGER.warn(ex.getMessage() + " Using SimpleLogger");
        return new SimpleLoggerContextFactory().getContext(FQCN, null, null, currentContext, null, null);
    }
}
 
Example #10
Source File: Logging.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static @Nullable org.apache.logging.log4j.Logger findLogger(String search) {
    for(LoggerContext context : getContexts()) {
        String name = Logging.findLogger(search, getLoggers(context).keySet());
        if(name != null) return (org.apache.logging.log4j.core.Logger) context.getLogger(name);
    }
    return null;
}
 
Example #11
Source File: LoggingCommands.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void enable() {
    // Verify this reflection magic works
    for(LoggerContext context : Logging.L4J.getContexts()) {
        Logging.L4J.getLoggers(context);
    }
}
 
Example #12
Source File: Logging.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static List<? extends LoggerContext> getContexts() {
    LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
    if(factory instanceof SimpleLoggerContextFactory) {
        return Collections.singletonList(factory.getContext(null, null, null, true));
    }
    return ((Log4jContextFactory) org.apache.logging.log4j.LogManager.getFactory()).getSelector().getLoggerContexts();
}
 
Example #13
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static ConcurrentMap<String, Logger> getLoggersMap(final LoggerContext context) {
    synchronized (CONTEXT_MAP) {
        ConcurrentMap<String, Logger> map = CONTEXT_MAP.get(context);
        if (map == null) {
            map = new ConcurrentHashMap<>();
            CONTEXT_MAP.put(context, map);
        }
        return map;
    }
}
 
Example #14
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public final Category getParent() {
    if (!isCoreAvailable) {
        return null;
    }
    org.apache.logging.log4j.Logger parent = CategoryUtil.getParent(logger);
    LoggerContext loggerContext = CategoryUtil.getLoggerContext(logger);
    if (parent == null || loggerContext == null) {
        return null;
    }
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(loggerContext);
    final Logger l = loggers.get(parent.getName());
    return l == null ? new Category(parent) : l;
}
 
Example #15
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
static Logger getInstance(final LoggerContext context, final String name, final LoggerFactory factory) {
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(context);
    Logger logger = loggers.get(name);
    if (logger != null) {
        return logger;
    }
    logger = factory.makeNewLoggerInstance(name);
    final Logger prev = loggers.putIfAbsent(name, logger);
    return prev == null ? logger : prev;
}
 
Example #16
Source File: LoggerContextTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanup() throws Exception {
    Log4jLoggerFactory factory = (Log4jLoggerFactory) StaticLoggerBinder.getSingleton().getLoggerFactory();
    factory.getLogger("test");
    Set<LoggerContext> set = factory.getLoggerContexts();
    LoggerContext ctx1 = set.toArray(new LoggerContext[0])[0];
    assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle);
    ((LifeCycle) ctx1).stop();
    set = factory.getLoggerContexts();
    assertTrue("Expected no LoggerContexts", set.isEmpty());
}
 
Example #17
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public static Logger exists(final String name) {
    final LoggerContext ctx = PrivateManager.getContext();
    if (!ctx.hasLogger(name)) {
        return null;
    }
    return Logger.getLogger(name);
}
 
Example #18
Source File: LoggerContextTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanup() throws Exception {
    Log4jLoggerFactory factory = (Log4jLoggerFactory) LoggerFactory.getILoggerFactory();
    factory.getLogger("test");
    Set<LoggerContext> set = factory.getLoggerContexts();
    LoggerContext ctx1 = set.toArray(new LoggerContext[0])[0];
    assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle);
    ((LifeCycle) ctx1).stop();
    set = factory.getLoggerContexts();
    assertTrue("Expected no LoggerContexts", set.isEmpty());
}
 
Example #19
Source File: LogManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public static Logger exists(final String name) {
    final LoggerContext ctx = PrivateManager.getContext();
    if (!ctx.hasLogger(name)) {
        return null;
    }
    return Logger.getLogger(name);
}
 
Example #20
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static ConcurrentMap<String, Logger> getLoggersMap(final LoggerContext context) {
    synchronized (CONTEXT_MAP) {
        ConcurrentMap<String, Logger> map = CONTEXT_MAP.get(context);
        if (map == null) {
            map = new ConcurrentHashMap<>();
            CONTEXT_MAP.put(context, map);
        }
        return map;
    }
}
 
Example #21
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public final Category getParent() {
    if (!isCoreAvailable) {
        return null;
    }
    org.apache.logging.log4j.Logger parent = CategoryUtil.getParent(logger);
    LoggerContext loggerContext = CategoryUtil.getLoggerContext(logger);
    if (parent == null || loggerContext == null) {
        return null;
    }
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(loggerContext);
    final Logger l = loggers.get(parent.getName());
    return l == null ? new Category(parent) : l;
}
 
Example #22
Source File: SimplePerfTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private static void workAroundLog4j2_5Bug() {
    // use reflection so we can use the same test with older versions of log4j2
    try {
        final Method setUseThreadLocals =
                AsyncLoggerContext.class.getDeclaredMethod("setUseThreadLocals", new Class[]{boolean.class});
        final LoggerContext context = LogManager.getContext(false);
        setUseThreadLocals.invoke(context, new Object[] {Boolean.TRUE});
    } catch (final Throwable ignored) {
    }
}
 
Example #23
Source File: Category.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
static Logger getInstance(final LoggerContext context, final String name, final PrivateAdapter factory) {
    final ConcurrentMap<String, Logger> loggers = getLoggersMap(context);
    Logger logger = loggers.get(name);
    if (logger != null) {
        return logger;
    }
    logger = factory.newLogger(name, context);
    final Logger prev = loggers.putIfAbsent(name, logger);
    return prev == null ? logger : prev;
}
 
Example #24
Source File: LogAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected Log newLogger(final String name, final LoggerContext context) {
    return new Log4jLog(context.getLogger(name));
}
 
Example #25
Source File: Category.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public static LoggerContext getContext() {
    return getContext(FQCN, false);
}
 
Example #26
Source File: SLF4JLoggerContextFactory.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public LoggerContext getContext(final String fqcn, final ClassLoader loader, final Object externalContext,
                                final boolean currentContext, final URI configLocation, final String name) {
    return context;
}
 
Example #27
Source File: ApiLoggerAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected Logger newLogger(final String name, final LoggerContext context) {
    return new ApiLogger(context.getLogger(name, MESSAGE_FACTORY));
}
 
Example #28
Source File: Category.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected org.apache.logging.log4j.spi.LoggerContext getContext() {
    return PrivateManager.getContext();
}
 
Example #29
Source File: Category.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
static Logger getInstance(final LoggerContext context, @SuppressWarnings("rawtypes") final Class clazz) {
    return getInstance(context, clazz.getName());
}
 
Example #30
Source File: Category.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
static Logger getRoot(final LoggerContext context) {
    return getInstance(context, Strings.EMPTY);
}