大家可以松一口气了,这个组件比较简单,这一节和前面几节想比,也简单的多。
Logger接口
Tomcat中的日志记录器都必须实现org.apache.catalina.Logger接口。
package org.apache.catalina; import java.beans.PropertyChangeListener; public interface Logger { public static final int FATAL = Integer.MIN_VALUE; public static final int ERROR = 1; public static final int WARNING = 2; public static final int INFORMATION = 3; public static final int DEBUG = 4; public Container getContainer(); public void setContainer(Container container); public String getInfo(); public int getVerbosity(); public void setVerbosity(int verbosity); public void addPropertyChangeListener(PropertyChangeListener listener); public void log(String message); public void log(Exception exception, String msg); public void log(String message, Throwable throwable); public void log(String message, int verbosity); public void log(String message, Throwable throwable, int verbosity); public void removePropertyChangeListener(PropertyChangeListener listener); }
Logger定义了五种日志记录基本,当我们在调用log(String message, int verbosity)方法记录日志的时候,只有传进来的verbosity小于等于系统的默认值才会记录。话说大家知道Integer.MIN_VALUE等于多少吗?大概负的21亿左右吧。setVerbosity与getVerbosity是干什么的还需要我说么?
Tomcat的日志记录器
LoggerBase类(抽象类)
它实现了Logger接口中除log(String message)方法外的其余方法;
在类中默认了日志记录级别为
protected int verbosity = ERROR;
但是我们可以通过setVerbosity(int verbosity)来重新设置记录级别;
看看定义的接受日志级别的两个log方法
public void log(String message, int verbosity) { if (this.verbosity >= verbosity) log(message); } public void log(String message, Throwable throwable, int verbosity) { if (this.verbosity >= verbosity) log(message, throwable); }
SystemOutLogger类
看名字就知道,信息最后输出到控制台。
SysteErrLogger类
看名字就知道,它是错误输出,信息仍在控制台,不过是红色的。
FileLogger类
看名字就知道,它是吧信息输出的文件里。
不过具体的来说,里面还是有点说头的,慢慢来。
首先FileLogger还实现了Lifecycle接口,因此它可以像其他组件一样被父组件所启动。
在本节中FileLogger的start与stop方法其实只是改变了布尔值started而已,没有做其他的,另一方面,FileLogger的start与stop方法也并没有被调用!
现在我们就看看怎么给文件里写信息。
public void log(String msg) { // Construct the timestamp we will use, if requested Timestamp ts = new Timestamp(System.currentTimeMillis()); String tsString = ts.toString().substring(0, 19); String tsDate = tsString.substring(0, 10); System.out.println("tsString "+tsString); //tsString 2014-10-20 15:25:27 System.out.println("ts "+ts); //ts 2014-10-20 15:25:27.406 System.out.println("tsDate "+tsDate); //tsDate 2014-10-20 // If the date has changed, switch log files if (!date.equals(tsDate)) { synchronized (this) { if (!date.equals(tsDate)) { close(); date = tsDate; open(); } } } // Log this message, timestamped if necessary if (writer != null) { if (timestamp) { writer.println(tsString + " " + msg); } else { writer.println(msg); } } }
File默认是每一天一个新的文件,文件内格式就是日期+具体信息(如果timestamp为true的话);
那close(),open()方法和writer属性又是什么呢?
private void open() { // Create the directory if necessary File dir = new File(directory); if (!dir.isAbsolute()) dir = new File(System.getProperty("catalina.base"), directory); //至于catalina.base是什么 一会再说 dir.mkdirs(); // Open the current log file try { String pathname = dir.getAbsolutePath() + File.separator + prefix + date + suffix; writer = new PrintWriter(new FileWriter(pathname, true), true); } catch (IOException e) { writer = null; } }
先检查给定的目录是否存在,如果不存在新建一个;接着根据前缀,时间,后缀设定文件名;根据文件名填充writer对象。
private void close() { if (writer == null) return; writer.flush(); writer.close(); writer = null; date = ""; }
FileLogger中的date代表的就是当前的时间,当然关闭后为空。
应用程序
Bootstrap类
... System.setProperty("catalina.base", System.getProperty("user.dir")); FileLogger logger = new FileLogger(); logger.setPrefix("FileLog_"); logger.setSuffix(".txt"); logger.setTimestamp(true); logger.setDirectory("webroot"); context.setLogger(logger); ....
SimpleContext类
public synchronized void start() throws LifecycleException { log("starting Context"); ........ lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null); log("Context started"); } private void log(String message) { Logger logger = this.getLogger(); if (logger!=null) logger.log(message); }
运行后在项目的webroot目录下就可以看到FileLog_2014-10-20.txt文件;
每次运行,在文件中都会多出如下的内容
2014-10-20 14:34:21 HttpConnector Opening server socket on all host IP addresses
2014-10-20 14:34:22 HttpConnector[8080] Starting background thread
2014-10-20 14:34:22 HttpProcessor[8080][0] Starting background thread
2014-10-20 14:34:22 HttpProcessor[8080][1] Starting background thread
2014-10-20 14:34:22 HttpProcessor[8080][2] Starting background thread
2014-10-20 14:34:22 HttpProcessor[8080][3] Starting background thread
2014-10-20 14:34:22 HttpProcessor[8080][4] Starting background thread
2014-10-20 14:34:22 starting Context
2014-10-20 14:34:22 Context started