@@ -29,11 +29,20 @@ public class Red5LoggerFactory {
2929
3030 public static boolean DEBUG = true ;
3131
32+ // root logger
33+ private static Logger rootLogger ;
34+
35+ // context selector
36+ private static ContextSelector contextSelector ;
37+
3238 static {
3339 DEBUG = Boolean .valueOf (System .getProperty ("logback.debug" , "false" ));
3440 try {
35- Logger logger = LoggerFactory .getILoggerFactory ().getLogger (Logger .ROOT_LOGGER_NAME );
36- logger .debug ("Red5LoggerFactory instanced by Thread: {}" , Thread .currentThread ().getName ());
41+ rootLogger = LoggerFactory .getILoggerFactory ().getLogger (Logger .ROOT_LOGGER_NAME );
42+ rootLogger .debug ("Red5LoggerFactory instanced by Thread: {}" , Thread .currentThread ().getName ());
43+ rootLogger .debug ("Logging context selector: {} impl: {}" , System .getProperty ("logback.ContextSelector" ), getContextSelector ());
44+ // get the context selector here
45+ contextSelector = getContextSelector ();
3746 } catch (Throwable t ) {
3847 t .printStackTrace ();
3948 }
@@ -43,7 +52,7 @@ public static Logger getLogger(Class<?> clazz) {
4352 if (DEBUG ) {
4453 System .out .printf ("getLogger for: %s thread: %s%n" , clazz .getName (), Thread .currentThread ().getName ());
4554 ClassLoader cl = Thread .currentThread ().getContextClassLoader ();
46- System . out . printf ( "class loader: %s%n " , cl );
55+ rootLogger . debug ( "Class loader: {} " , cl );
4756 // if cl is WebappClassLoader type we can probably get the context from it
4857 //if (cl instanceof WebappClassLoader) {
4958 // getContextName()
@@ -52,37 +61,22 @@ public static Logger getLogger(Class<?> clazz) {
5261 Logger logger = null ;
5362 if (useLogback ) {
5463 // determine the red5 app name or servlet context name
55- String contextName = CoreConstants . DEFAULT_CONTEXT_NAME ;
64+ final String threadName = Thread . currentThread (). getName () ;
5665 // route the Launcher entries to the correct context
57- String [] parts = Thread .currentThread ().getName ().split ("Loader:/" );
58- if (parts .length > 1 ) {
59- contextName = parts [1 ];
66+ if (threadName .startsWith ("Loader:/" )) {
67+ String contextName = threadName .split ("Loader:/" )[1 ];
68+ logger = getLogger (clazz , contextName );
69+ } else {
70+ logger = getLogger (clazz , CoreConstants .DEFAULT_CONTEXT_NAME );
6071 }
61- logger = Red5LoggerFactory .getLogger (clazz , contextName );
62- /*
63- * // get a reference to our caller Class caller = Thread.currentThread().getStackTrace()[2].getClassName(); if (DEBUG) { System.out.printf("Caller class: %s classloader: %s%n",
64- * caller, caller.getClassLoader()); } // if the incoming class extends StatefulScopeWrappingAdapter we lookup the context by scope name boolean scopeAware =
65- * StatefulScopeWrappingAdapter.class.isAssignableFrom(caller); if (DEBUG) { System.out.printf("scopeAware: %s%n", scopeAware); } if (scopeAware) { try { Class wrapper = null; if
66- * ((wrapper = caller.asSubclass(StatefulScopeWrappingAdapter.class)) != null) { Method getScope = wrapper.getMethod("getScope", new Class[0]); // NPE will occur here if the scope
67- * is not yet set on the application adapter IScope scope = (IScope) getScope.invoke(null, new Object[0]); if (DEBUG) { System.out.printf("scope: %s%n", scope); } contextName =
68- * scope.getName(); } } catch (Exception cce) { //cclog.warn("Exception {}", e); } } else { // if the incoming class is a servlet we lookup the context name boolean
69- * servletScopeAware = Servlet.class.isAssignableFrom(caller); if (DEBUG) { System.out.printf("servletScopeAware: %s%n", servletScopeAware); } if (servletScopeAware) { try { Class
70- * wrapper = null; if ((wrapper = caller.asSubclass(Servlet.class)) != null) { //ServletConfig getServletConfig Method getServletConfig = wrapper.getMethod("getServletConfig", new
71- * Class[0]); // NPE will occur here if the scope is not yet set on the application adapter ServletConfig config = (ServletConfig) getServletConfig.invoke(null, new Object[0]); if
72- * (DEBUG) { System.out.printf("config: %s%n", config); } contextName = config.getServletContext().getContextPath().replaceAll("/", ""); if ("".equals(contextName)) { contextName =
73- * "root"; } } } catch (Exception cce) { //cclog.warn("Exception {}", e); } } else { // route the Launcher entries to the correct context String[] parts =
74- * Thread.currentThread().getName().split("Loader:/"); if (parts.length > 1) { contextName = parts[1]; } else { contextName = CoreConstants.DEFAULT_CONTEXT_NAME; } } } logger =
75- * Red5LoggerFactory.getLogger(clazz, contextName);
76- */
7772 }
7873 if (logger == null ) {
7974 logger = LoggerFactory .getLogger (clazz );
8075 }
8176 return logger ;
8277 }
8378
84- @ SuppressWarnings ({ "rawtypes" })
85- public static Logger getLogger (Class clazz , String contextName ) {
79+ public static Logger getLogger (Class <?> clazz , String contextName ) {
8680 return getLogger (clazz .getName (), contextName );
8781 }
8882
@@ -97,22 +91,22 @@ public static Logger getLogger(String name, String contextName) {
9791 contextName = CoreConstants .DEFAULT_CONTEXT_NAME ;
9892 }
9993 try {
100- ContextSelector selector = Red5LoggerFactory .getContextSelector ();
10194 // get the context for the given context name or default if null
102- LoggerContext context = selector .getLoggerContext (contextName );
95+ LoggerContext context = contextSelector .getLoggerContext (contextName );
10396 // and if we get here, fall back to the default context
10497 if (context == null ) {
10598 System .err .printf ("No context named %s was found!!%n" , contextName );
10699 }
107100 // get the logger from the context or default context
108101 if (context != null ) {
109102 logger = context .getLogger (name );
110- // System.out.printf("Application name: %s in context: %s%n", context.getProperty(KEY_APP_NAME), contextName);
103+ if (DEBUG ) {
104+ rootLogger .debug ("Application name: {} in context: {}" , context .getProperty (CoreConstants .CONTEXT_NAME_KEY ), contextName );
105+ }
111106 }
112107 } catch (Exception e ) {
113108 // no logback, use whatever logger is in-place
114- System .err .printf ("Exception %s%n" , e .getMessage ());
115- e .printStackTrace ();
109+ rootLogger .error ("Exception {}" , e );
116110 }
117111 }
118112 if (logger == null ) {
@@ -122,26 +116,26 @@ public static Logger getLogger(String name, String contextName) {
122116 }
123117
124118 public static ContextSelector getContextSelector () {
119+ ContextSelector selector = null ;
125120 if (useLogback ) {
126121 ContextSelectorStaticBinder contextSelectorBinder = ContextSelectorStaticBinder .getSingleton ();
127- ContextSelector selector = contextSelectorBinder .getContextSelector ();
122+ selector = contextSelectorBinder .getContextSelector ();
128123 if (selector == null ) {
129124 if (DEBUG ) {
130- System . err . println ("Context selector was null, creating default context" );
125+ rootLogger . error ("Context selector was null, creating default context" );
131126 }
132127 LoggerContext defaultLoggerContext = new LoggerContext ();
133128 defaultLoggerContext .setName (CoreConstants .DEFAULT_CONTEXT_NAME );
134129 try {
135130 contextSelectorBinder .init (defaultLoggerContext , null );
136131 selector = contextSelectorBinder .getContextSelector ();
132+ rootLogger .debug ("Context selector: {}" , selector .getClass ().getName ());
137133 } catch (Exception e ) {
138- e . printStackTrace ( );
134+ rootLogger . error ( "Exception {}" , e );
139135 }
140136 }
141- //System.out.printf("Context selector: %s%n", selector.getClass().getName());
142- return selector ;
143137 }
144- return null ;
138+ return selector ;
145139 }
146140
147141 public static void setUseLogback (boolean useLogback ) {
0 commit comments