其实tomcat使用了JDK的Logger类来实现的
第一:为每一个类健一个日志类的对象如下: private static Log log = LogFactory.getLog(StandardServer.class); LogFactory.java类里的实现过程如下: private static LogFactory singleton=new LogFactory(); public static Log getLog(Class clazz) throws LogConfigurationException { return (getFactory().getInstance(clazz)); } public static LogFactory getFactory() throws LogConfigurationException { return singleton; } public Log getInstance(Class clazz) throws LogConfigurationException { return getInstance( clazz.getName()); } public Log getInstance(String name) throws LogConfigurationException { return DirectJDKLog.getInstance(name); } DirectJDKLog.java类里的实现如下: static Log getInstance(String name) { return new DirectJDKLog( name ); } public DirectJDKLog(String name ) { logger=Logger.getLogger(name); } 好了,接下来就可以使用日志类了 public void start() throws LifecycleException { // Validate and update our current component state if (started) { log.debug(sm.getString("standardServer.start.started")); return; } DirectJDKLog.java中实现日志各个级别的打印: public final void debug/info/trace/warn(Object message) { log(Level.FINE, String.valueOf(message), null); } 主要工作的地方来了…… private void log( Level level, String msg, Throwable ex ) { if (logger.isLoggable(level)) { // Hack (?) to get the stack trace. Throwable dummyException=new Throwable(); StackTraceElement locations[]=dummyException.getStackTrace(); // Caller will be the third element String cname="unknown"; String method="unknown"; if( locations!=null && locations.length >2 ) { StackTraceElement caller=locations[2]; cname=caller.getClassName(); method=caller.getMethodName(); } if( ex==null ) { logger.logp( level, cname, method, msg ); } else { logger.logp( level, cname, method, msg, ex ); } } } 这个函数里有一个见解的地方,就是tomcat获取堆栈信息,我们在自己的项目中可以根据实际情况来进行某个位置的堆栈分析等工作,,