Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional ways to specify contextselector updated2 #671

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class ClassicConstants {
+ "comp/env/logback/configuration-resource";
public static final String JNDI_CONTEXT_NAME = JNDI_JAVA_NAMESPACE + "comp/env/logback/context-name";

/** JNDI name of custom contextSelector classname, if used */
public static final String JNDI_LOGBACK_CONTEXT_SELECTOR = JNDI_JAVA_NAMESPACE + "comp/env/logback/contextSelector";

/**
* The maximum number of package separators (dots) that abbreviation algorithms
* can handle. Class or logger names with more separators will have their first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import javax.naming.Context;
import javax.naming.NamingException;

import ch.qos.logback.classic.ClassicConstants;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.selector.ContextJNDISelector;
import ch.qos.logback.classic.selector.ContextSelector;
import ch.qos.logback.classic.selector.DefaultContextSelector;
import ch.qos.logback.core.util.JNDIUtil;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;

Expand Down Expand Up @@ -60,6 +64,21 @@ public void init(LoggerContext defaultLoggerContext, Object key) throws ClassNot
}

String contextSelectorStr = OptionHelper.getSystemProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR);

// If not there, then try the System properties
if (contextSelectorStr == null) {
contextSelectorStr = OptionHelper.getEnv(ClassicConstants.LOGBACK_CONTEXT_SELECTOR);
}

// If still null, check for a custom selector class specified by JNDI (this is not the same as specifying "JNDI")
if (contextSelectorStr == null) {
try {
Context ctx = JNDIUtil.getInitialContext();
contextSelectorStr = JNDIUtil.lookupString(ctx, ClassicConstants.JNDI_LOGBACK_CONTEXT_SELECTOR);
} catch (NamingException ne) {
}
}

if (contextSelectorStr == null) {
contextSelector = new DefaultContextSelector(defaultLoggerContext);
} else if (contextSelectorStr.equals("JNDI")) {
Expand Down