Skip to content
Permalink
Browse files

[WFCORE-4359] Ensure the CLI embedded stdio and log contexts are used…

… for CLI loggers. This ensures all CLI loggers write to the log manager configured for CLI and embedded server loggers write to the server configured loggers.
  • Loading branch information
jamezp committed Mar 5, 2019
1 parent 0d82f44 commit 50b983f127bd008cad30fe17d33b8578521887d5
@@ -26,10 +26,14 @@
import org.jboss.logmanager.LogContextSelector;
import org.jboss.stdio.StdioContext;
import org.jboss.stdio.StdioContextSelector;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
* {@link org.jboss.stdio.StdioContextSelector} and {@link org.jboss.logmanager.LogContextSelector}
* that uses an {@link java.lang.InheritableThreadLocal} as a source of the contexts.
* <p>
* Note that if the logger is a CLI logger the default contexts will be used regardless of the thread-local contexts.
* </p>
*
* @author Brian Stansberry (c) 2015 Red Hat Inc.
*/
@@ -39,6 +43,7 @@

private final Contexts localContexts;
private final Contexts defaultContexts;
private final ClassLoader cliClassLoader;

ThreadLocalContextSelector(Contexts local, Contexts defaults) {
assert local != null;
@@ -48,6 +53,7 @@
assert defaults.getLogContext() != null;
this.localContexts = local;
this.defaultContexts = defaults;
cliClassLoader = ThreadLocalContextSelector.class.getClassLoader();
}

Contexts pushLocal() {
@@ -62,13 +68,22 @@ void restore(Contexts toRestore) {

@Override
public StdioContext getStdioContext() {
// CLI loggers should only use the default stdio context regardless if the thread-local context is set.
if (WildFlySecurityManager.getCurrentContextClassLoaderPrivileged().equals(cliClassLoader)) {
return defaultContexts.getStdioContext();
}
Contexts threadContext = threadLocal.get();
StdioContext local = threadContext != null ? threadContext.getStdioContext() : null;
return local == null ? defaultContexts.getStdioContext() : local;
}

@Override
public LogContext getLogContext() {
// CLI loggers should only use the default stdio context regardless if the thread-local context is set This
// allows the context configured for CLI, e.g. jboss-cli-logging.properties.
if (WildFlySecurityManager.getCurrentContextClassLoaderPrivileged().equals(cliClassLoader)) {
return defaultContexts.getLogContext();
}
Contexts threadContext = threadLocal.get();
LogContext local = threadContext != null ? threadContext.getLogContext() : null;
return local == null ? defaultContexts.getLogContext() : local;
@@ -112,12 +112,22 @@
class Factory {
private static final LogContext EMBEDDED_LOG_CONTEXT = LogContext.create();

/**
* Creates a new selector which wraps the current {@linkplain LogContext#getLogContextSelector() selector}.
*
* @return a new selector that wraps the current selector
*/
public static WildFlyLogContextSelector create() {
// Use the current log context as the default, not LogContext.DEFAULT_LOG_CONTEXT_SELECTOR
// This allows embedding use cases to control the log context
return new WildFlyLogContextSelectorImpl(LogContext.getLogContext());
// Wrap the current LogContextSelector. This will be used as the default in the cases where this selector
// does not find a log context.
return new WildFlyLogContextSelectorImpl(LogContext.getLogContextSelector());
}

/**
* Creates a new selector which by default returns a static embedded context which can be used.
*
* @return a new selector
*/
public static WildFlyLogContextSelector createEmbedded() {
clearLogContext();
return new WildFlyLogContextSelectorImpl(EMBEDDED_LOG_CONTEXT);
@@ -41,13 +41,17 @@
private final AtomicInteger counter;

WildFlyLogContextSelectorImpl(final LogContext defaultLogContext) {
counter = new AtomicInteger(0);
contextSelector = new ClassLoaderLogContextSelector(new LogContextSelector() {
this(new ClassLoaderLogContextSelector(new LogContextSelector() {
@Override
public LogContext getLogContext() {
return defaultLogContext;
}
}, true);
}));
}

WildFlyLogContextSelectorImpl(final LogContextSelector defaultLogContextSelector) {
counter = new AtomicInteger(0);
contextSelector = new ClassLoaderLogContextSelector(defaultLogContextSelector, true);
threadLocalContextSelector = new ThreadLocalLogContextSelector(contextSelector);
}

0 comments on commit 50b983f

Please sign in to comment.
You can’t perform that action at this time.