Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.classic.selector;
 
 import static ch.qos.logback.classic.ClassicConstants.JNDI_CONFIGURATION_RESOURCE;
 import static ch.qos.logback.classic.ClassicConstants.JNDI_CONTEXT_NAME;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
 
A class that allows the LoggerFactory to access an environment-based LoggerContext.

To add in catalina.sh

JAVA_OPTS="$JAVA_OPTS "-Dlogback.ContextSelector=JNDI""

Author(s):
Ceki Gülcü
Sébastien Pennec
 
 public class ContextJNDISelector implements ContextSelector {
 
   private final LoggerContext defaultContext;
 
   private static final ThreadLocal<LoggerContextthreadLocal = new ThreadLocal<LoggerContext>();
 
   public ContextJNDISelector(LoggerContext context) {
      = Collections
             .synchronizedMap(new HashMap<StringLoggerContext>());
      = context;
   }
 
     return ;
   }
 
   public LoggerContext detachLoggerContext(String loggerContextName) {
     return .remove(loggerContextName);
   }
 
   public LoggerContext getLoggerContext() {
     String contextName = null;
     Context ctx = null;
 
     // First check if ThreadLocal has been set already
     LoggerContext lc = .get();
     if (lc != null) {
       return lc;
     }
 
     try {
       // We first try to find the name of our
       // environment's LoggerContext
       ctx = JNDIUtil.getInitialContext();
       contextName = (String) JNDIUtil.lookup(ctx);
     } catch (NamingException ne) {
       // We can't log here
     }
 
     if (contextName == null) {
       // We return the default context
       return ;
     } else {
       // Let's see if we already know such a context
       LoggerContext loggerContext = .get(contextName);
 
       if (loggerContext == null) {
        // We have to create a new LoggerContext
        loggerContext = new LoggerContext();
        loggerContext.setName(contextName);
        .put(contextNameloggerContext);
        URL url = findConfigFileURL(ctxloggerContext);
        if (url != null) {
          configureLoggerContextByURL(loggerContexturl);
        } else {
          try {
            new ContextInitializer(loggerContext).autoConfig();
          } catch (JoranException je) {
          }
        }
        // logback-292
        if (!StatusUtil.contextHasStatusListener(loggerContext))
          StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
      }
      return loggerContext;
    }
  }
  private String conventionalConfigFileName(String contextName) {
    return "logback-" + contextName + ".xml";
  }
  private URL findConfigFileURL(Context ctxLoggerContext loggerContext) {
    StatusManager sm = loggerContext.getStatusManager();
    String jndiEntryForConfigResource = JNDIUtil.lookup(ctx,
            );
    // Do we have a dedicated configuration file?
    if (jndiEntryForConfigResource != null) {
      sm.add(new InfoStatus("Searching for [" + jndiEntryForConfigResource
              + "]"this));
      URL url = urlByResourceName(smjndiEntryForConfigResource);
      if (url == null) {
        String msg = "The jndi resource [" + jndiEntryForConfigResource
                + "] for context [" + loggerContext.getName()
                + "] does not lead to a valid file";
        sm.add(new WarnStatus(msgthis));
      }
      return url;
    } else {
      String resourceByConvention = conventionalConfigFileName(loggerContext
              .getName());
      return urlByResourceName(smresourceByConvention);
    }
  }
  private URL urlByResourceName(StatusManager smString resourceName) {
    sm.add(new InfoStatus("Searching for [" + resourceName + "]",
            this));
    URL url = Loader.getResource(resourceName, Loader.getTCL());
    if (url != null) {
      return url;
    }
    return Loader.getResourceBySelfClassLoader(resourceName);
  }
  private void configureLoggerContextByURL(LoggerContext contextURL url) {
    try {
      JoranConfigurator configurator = new JoranConfigurator();
      context.reset();
      configurator.setContext(context);
      configurator.doConfigure(url);
    } catch (JoranException e) {
    }
    StatusPrinter.printInCaseOfErrorsOrWarnings(context);
  }
  public List<StringgetContextNames() {
    List<Stringlist = new ArrayList<String>();
    return list;
  }
    return .get(name);
  }

  
Returns the number of managed contexts Used for testing purposes

Returns:
the number of managed contexts
  public int getCount() {
    return .size();
  }

  
These methods are used by the LoggerContextFilter.

They provide a way to tell the selector which context to use, thus saving the cost of a JNDI call at each new request.

Parameters:
context
  public void setLocalContext(LoggerContext context) {
    .set(context);
  }
  public void removeLocalContext() {
  }
New to GrepCode? Check out our FAQ X