public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector(); ContextJNDISelector sel = null; if (selector instanceof ContextJNDISelector) { sel = (ContextJNDISelector)selector; sel.setLocalContext(lc); } try { chain.doFilter(request, response); } finally { if (sel != null) { sel.removeLocalContext(); } } }
/** * Return the name of the current context name as found in the logging event. */ public String getDiscriminatingValue(ILoggingEvent event) { ContextSelector selector = ContextSelectorStaticBinder.getSingleton() .getContextSelector(); if (selector == null) { return defaultValue; } LoggerContext lc = selector.getLoggerContext(); if (lc == null) { return defaultValue; } return lc.getName(); }
public static ContextSelector getContextSelector() { if (useLogback) { ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder.getSingleton(); ContextSelector selector = contextSelectorBinder.getContextSelector(); if (selector == null) { if (DEBUG) { System.err.println("Context selector was null, creating default context"); } LoggerContext defaultLoggerContext = new LoggerContext(); defaultLoggerContext.setName(CoreConstants.DEFAULT_CONTEXT_NAME); try { contextSelectorBinder.init(defaultLoggerContext, null); selector = contextSelectorBinder.getContextSelector(); } catch (Exception e) { e.printStackTrace(); } } //System.out.printf("Context selector: %s%n", selector.getClass().getName()); return selector; } return null; }
public void contextDestroyed(ServletContextEvent servletContextEvent) { String loggerContextName = null; try { Context ctx = JNDIUtil.getInitialContext(); loggerContextName = (String) JNDIUtil.lookup(ctx, JNDI_CONTEXT_NAME); } catch (NamingException ne) { } if (loggerContextName != null) { System.out.println("About to detach context named " + loggerContextName); ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector(); if(selector == null) { System.out.println("Selector is null, cannot detach context. Skipping."); return; } LoggerContext context = selector.getLoggerContext(loggerContextName); if (context != null) { Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME); logger.warn("Stopping logger context " + loggerContextName); selector.detachLoggerContext(loggerContextName); // when the web-app is destroyed, its logger context should be stopped context.stop(); } else { System.out.println("No context named " + loggerContextName + " was found."); } } }
public static void bind() { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector(); if (selector instanceof LogbackContextSelector) { tlsLoggingContext.set(context); } }
@Override public void contextDestroyed(ServletContextEvent sce) { configWatchDog.interrupt(); ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector(); String contextId = HttpUtils.getContextId(sce.getServletContext()); selector.detachLoggerContext(contextId); configWatchDog.loggerContext.stop(); }
public static Logger getLogger(String name, String contextName) { if (DEBUG) { System.out.printf("getLogger for: %s in context: %s thread: %s%n", name, contextName, Thread.currentThread().getName()); } Logger logger = null; if (useLogback) { // disallow null context names if (contextName == null) { contextName = CoreConstants.DEFAULT_CONTEXT_NAME; } try { ContextSelector selector = Red5LoggerFactory.getContextSelector(); // get the context for the given context name or default if null LoggerContext context = selector.getLoggerContext(contextName); // and if we get here, fall back to the default context if (context == null) { System.err.printf("No context named %s was found!!%n", contextName); } // get the logger from the context or default context if (context != null) { logger = context.getLogger(name); // System.out.printf("Application name: %s in context: %s%n", context.getProperty(KEY_APP_NAME), contextName); } } catch (Exception e) { // no logback, use whatever logger is in-place System.err.printf("Exception %s%n", e.getMessage()); e.printStackTrace(); } } if (logger == null) { logger = LoggerFactory.getLogger(name); } return logger; }
public void contextInitialized(ServletContextEvent event) { ServletContext servletContext = event.getServletContext(); String contextName = servletContext.getContextPath().replaceAll("/", ""); if ("".equals(contextName)) { contextName = "root"; } System.out.printf("Context init: %s%n", contextName); ConfigurableWebApplicationContext appctx = (ConfigurableWebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (appctx != null) { System.out.printf("ConfigurableWebApplicationContext is not null in ContextLoggingListener for: %s, this indicates a misconfiguration or load order problem%n", contextName); } try { // get the selector ContextSelector selector = Red5LoggerFactory.getContextSelector(); // get the logger context for this servlet / app context by name URL url = servletContext.getResource(String.format("/WEB-INF/classes/logback-%s.xml", contextName)); if (url != null && Files.exists(Paths.get(url.toURI()))) { System.out.printf("Context logger config found: %s%n", url.toURI()); } else { url = servletContext.getResource("/WEB-INF/classes/logback.xml"); if (url != null && Files.exists(Paths.get(url.toURI()))) { System.out.printf("Context logger config found: %s%n", url.toURI()); } } // get the logger context for the servlet context LoggerContext loggerContext = url != null ? ((LoggingContextSelector) selector).getLoggerContext(contextName, url) : selector.getLoggerContext(contextName); // set the logger context for use elsewhere in the servlet context servletContext.setAttribute(Red5LoggerFactory.LOGGER_CONTEXT_ATTRIBUTE, loggerContext); // get the root logger for this context Logger logger = Red5LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME, contextName); logger.info("Starting up context: {}", contextName); } catch (Exception e) { System.err.printf("LoggingContextSelector is not the correct type: %s%n", e.getMessage()); e.printStackTrace(); } }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { LoggerContext context = (LoggerContext) request.getServletContext().getAttribute(Red5LoggerFactory.LOGGER_CONTEXT_ATTRIBUTE); // get the selector ContextSelector selector = Red5LoggerFactory.getContextSelector(); if (context != null) { // set the thread local ref ((LoggingContextSelector) selector).setLocalContext(context); } else { System.err.printf("No context named %s was found%n", contextName); } chain.doFilter(request, response); // remove the thread local ref so that log contexts dont use the wrong contextName ((LoggingContextSelector) selector).removeLocalContext(); }
public void contextDestroyed(ServletContextEvent event) { System.out.println("Context destroying..."); String contextName = pathToName(event); //System.out.printf("About to detach context named %s\n", contextName); ContextSelector selector = Red5LoggerFactory.getContextSelector(); LoggerContext context = selector.detachLoggerContext(contextName); if (context != null) { Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME); logger.debug("Shutting down context {}", contextName); context.reset(); } else { System.err.printf("No context named %s was found", contextName); } }
public ContextSelector getContextSelector() { return contextSelector; }
public ContextSelector getContextSelector() { return contextSelectorBinder.getContextSelector(); }
@SuppressWarnings({ "rawtypes" }) public static Logger getLogger(Class clazz, String contextName) { Logger logger = null; try { //check for logback Class cs = Class.forName("ch.qos.logback.classic.selector.ContextSelector"); //trigger an exception if the class doesn't actually exist cs.getDeclaredMethods(); // get the class for static binding cs = Class.forName("org.slf4j.impl.StaticLoggerBinder"); // get its declared methods Method[] methods = cs.getDeclaredMethods(); for (Method method : methods) { //ensure method exists if (method.getName().equals("getContextSelector")) { //System.out.println("Logger context selector method found"); //get the context selector StaticLoggerBinder binder = StaticLoggerBinder.getSingleton(); Method m1 = binder.getClass().getMethod("getContextSelector", (Class[]) null); ContextSelector selector = (ContextSelector) m1.invoke(binder, (Object[]) null); //get the context for the given context name or default if null LoggerContext ctx = null; if (contextName != null && contextName.length() > 0) { ctx = selector.getLoggerContext(contextName); } // and if we get here, fall back to the default context if (ctx == null) { ctx = selector.getLoggerContext(); } //debug //StatusPrinter.print(ctx); //get the logger from the context or default context logger = ((ctx != null) ? ctx.getLogger(clazz) : selector.getDefaultLoggerContext().getLogger(clazz)); break; } } } catch (Exception e) { //no logback, use whatever logger is in-place System.err.printf("Exception %s", e.getMessage()); } if (logger == null) { //no logback, use whatever logger is in-place logger = LoggerFactory.getLogger(clazz); } return logger; }
@SuppressWarnings({ "rawtypes" }) public static Logger getLogger(String name, String contextName) { Logger logger = null; try { //check for logback Class cs = Class.forName("ch.qos.logback.classic.selector.ContextSelector"); //trigger an exception if the class doesn't actually exist cs.getDeclaredMethods(); // get the class for static binding cs = Class.forName("org.slf4j.impl.StaticLoggerBinder"); // get its declared methods Method[] methods = cs.getDeclaredMethods(); for (Method method : methods) { //ensure method exists if (method.getName().equals("getContextSelector")) { //System.out.println("Logger context selector method found"); //get the context selector StaticLoggerBinder binder = StaticLoggerBinder.getSingleton(); Method m1 = binder.getClass().getMethod("getContextSelector", (Class[]) null); ContextSelector selector = (ContextSelector) m1.invoke(binder, (Object[]) null); //get the context for the given context name or default if null LoggerContext ctx = null; if (contextName != null && contextName.length() > 0) { ctx = selector.getLoggerContext(contextName); } // and if we get here, fall back to the default context if (ctx == null) { ctx = selector.getLoggerContext(); } //debug //StatusPrinter.print(ctx); //get the logger from the context or default context logger = ((ctx != null) ? ctx.getLogger(name) : selector.getDefaultLoggerContext().getLogger(name)); break; } } } catch (Exception e) { //no logback, use whatever logger is in-place System.err.printf("Exception %s", e.getMessage()); } if (logger == null) { //no logback, use whatever logger is in-place logger = LoggerFactory.getLogger(name); } return logger; }
/** * Instantiate the context selector class designated by the user. The selector * must have a constructor taking a LoggerContext instance as an argument. * * @param defaultLoggerContext * @param contextSelectorStr * @return an instance of the designated context selector class * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ static ContextSelector dynamicalContextSelector( LoggerContext defaultLoggerContext, String contextSelectorStr) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> contextSelectorClass = Loader.loadClass(contextSelectorStr); Constructor cons = contextSelectorClass .getConstructor(new Class[] { LoggerContext.class }); return (ContextSelector) cons.newInstance(defaultLoggerContext); }
public ContextSelector getContextSelector();