Tomcat源代码学习日记(2004-09-30 11:44)

类别:软件工程 点击:0 评论:0 推荐:

Tomcat的log

在Catalina 的start中
 public void start() {
        if (server == null) {
            load();
        }
        long t1 = System.currentTimeMillis();
        // Start the new server
        if (server instanceof Lifecycle) {
            try {
                ((Lifecycle) server).start();
            } catch (LifecycleException e) {
                log.error("Catalina.start: ", e);
            }
        }

        long t2 = System.currentTimeMillis();
        log.info("Server startup in " + (t2 - t1) + " ms");

        try {
            // Register shutdown hook
            if (useShutdownHook) {
                if (shutdownHook == null) {
                    shutdownHook = new CatalinaShutdownHook();
                }
                Runtime.getRuntime().addShutdownHook(shutdownHook);
            }
        } catch (Throwable t) {
            // This will fail on JDK 1.2. Ignoring, as Tomcat can run
            // fine without the shutdown hook.
        }

        if (await) {
            await();
            stop();
        }
}

 if (shutdownHook == null) {
                    shutdownHook = new CatalinaShutdownHook();
                }

protected class CatalinaShutdownHook extends Thread {
        public void run() {
            if (server != null) {
                Catalina.this.stop();
            }
        }  
  }    
    private static org.apache.commons.logging.Log log=
        org.apache.commons.logging.LogFactory.getLog( Catalina.class );
}

public static Log getLog(Class clazz)
        throws LogConfigurationException {
        return (getFactory().getInstance(clazz));
    }

public static LogFactory getFactory() throws LogConfigurationException {
        ClassLoader contextClassLoader =
            (ClassLoader)AccessController.doPrivileged(
                new PrivilegedAction() {
                    public Object run() {
                        return getContextClassLoader();
                    }
                });
        LogFactory factory = getCachedFactory(contextClassLoader);
        if (factory != null)
            return factory;
        Properties props=null;
        try {
            InputStream stream = getResourceAsStream(contextClassLoader,
                                                     FACTORY_PROPERTIES);
            if (stream != null) {
                props = new Properties();
                props.load(stream);
                stream.close();
            }
        } catch (IOException e) {
        } catch (SecurityException e) {
        }
        try {
            String factoryClass = System.getProperty(FACTORY_PROPERTY);
            if (factoryClass != null) {
                factory = newFactory(factoryClass, contextClassLoader);
            }
        } catch (SecurityException e) {
            ; 
        }
        if (factory == null) {
            try {
                InputStream is = getResourceAsStream(contextClassLoader,
                                                     SERVICE_ID);
                if( is != null ) {
                    BufferedReader rd;
                    try {
                        rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    } catch (java.io.UnsupportedEncodingException e) {
                        rd = new BufferedReader(new InputStreamReader(is));
                    }

                    String factoryClassName = rd.readLine();
                    rd.close();
                    if (factoryClassName != null &&
                        ! "".equals(factoryClassName)) {
                        factory= newFactory( factoryClassName, contextClassLoader );
                    }
                }
            } catch( Exception ex ) {
                ;
            }
        }
       if (factory == null  &&  props != null) {
            String factoryClass = props.getProperty(FACTORY_PROPERTY);
            if (factoryClass != null) {
                factory = newFactory(factoryClass, contextClassLoader);
            }
        }
        if (factory == null) {
            factory = newFactory(FACTORY_DEFAULT, LogFactory.class.getClassLoader());
        }
        if (factory != null) {
            cacheFactory(contextClassLoader, factory);
            if( props!=null ) {
                Enumeration names = props.propertyNames();
                while (names.hasMoreElements()) {
                    String name = (String) names.nextElement();
                    String value = props.getProperty(name);
                    factory.setAttribute(name, value);
                }
            }
        }
        return factory;
    }

LogFactory factory = getCachedFactory(contextClassLoader);

private static LogFactory getCachedFactory(ClassLoader contextClassLoader)
    {
        LogFactory factory = null;
        if (contextClassLoader != null)
            factory = (LogFactory) factories.get(contextClassLoader);
        return factory;
    }




 

本文地址:http://com.8s8s.com/it/it35417.htm