protected LoggerContext getDefault() { final LoggerContext ctx = DEFAULT_CONTEXT.get(); if (ctx != null) { return ctx; } DEFAULT_CONTEXT.compareAndSet(null, createContext(defaultContextName(), null)); return DEFAULT_CONTEXT.get(); }
@Override public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext) { return getContext(fqcn, loader, currentContext, null); }
@Override public LoggerContext getContext(final String fqcn, final ClassLoader loader, final boolean currentContext, final URI configLocation) { if (currentContext) { final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); if (ctx != null) { return ctx; } return getDefault(); } else if (loader != null) { return locateContext(loader, configLocation); } else { final Class<?> clazz = StackLocatorUtil.getCallerClass(fqcn); if (clazz != null) { return locateContext(clazz.getClassLoader(), configLocation); } final LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); if (lc != null) { return lc; } return getDefault(); } }
private LoggerContext locateContext(final ClassLoader loaderOrNull, final URI configLocation) { final String name = toContextMapKey(loader); AtomicReference<WeakReference<LoggerContext>> ref = CONTEXT_MAP.get(name); if (ref == null) { while (parent != null) { ref = CONTEXT_MAP.get(toContextMapKey(parent)); if (ref != null) { final WeakReference<LoggerContext> r = ref.get(); LoggerContext ctx = createContext(name, configLocation); final AtomicReference<WeakReference<LoggerContext>> r = new AtomicReference<>(); r.set(new WeakReference<>(ctx)); ctx = createContext(name, configLocation); ref.compareAndSet(weakRef, new WeakReference<>(ctx)); return ctx;
private static ContextSelector createContextSelector() { try { final ContextSelector selector = LoaderUtil.newCheckedInstanceOfProperty(Constants.LOG4J_CONTEXT_SELECTOR, ContextSelector.class); if (selector != null) { return selector; } } catch (final Exception e) { LOGGER.error("Unable to create custom ContextSelector. Falling back to default.", e); } return new ClassLoaderContextSelector(); }