标签:cat print files value open() work 图片 变化 概述
顾名思义,这一章将的是tomcat的日志记录器组件,比较简单,tomcat提供了几种类型不同的日志记录器,但是所有的日志记录器都需要实现Logger接口,Logger的定义如下
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();
//与container绑定
public void setContainer(Container container);
public String getInfo();
public int getVerbosity();
//设置日志级别
public void setVerbosity(int verbosity);
public void addPropertyChangeListener(PropertyChangeListener listener);
//重载了log方法,不同的logger实现实现了不同的log方法
public void log(String message);
public void log(Exception exception, String msg);
public void log(String message, Throwable throwable);
//这两个log方法都接收一个verbosity参数,如果该参数的级别数字比该日志记录器的级别数字小,才会记录
//该日志,好比说DEBUG级别的日志记录器可以记录所有级别的日志
public void log(String message, int verbosity);
public void log(String message, Throwable throwable, int verbosity);
public void removePropertyChangeListener(PropertyChangeListener listener);
}
tomcat提供了三种具体实现,FileLogger、SystemErrLogger、SystemOutLogger,这三者与Logger的UML图如下:
LoggerBase为抽象类,它实现了除了log(String msg)
之外的所有方法,而具体的实现类则根据自己的特点实现了不同的log(String msg)
方法,此外LoggerBase的默认日志级别是ERROR
protected int verbosity = ERROR;
可以调用setVerbosity(int verbosity)
来设置
下面来看,三个不同的log方法的实现
SystemOutLogger类
public void log(String msg) {
System.out.println(msg);
}
SystemErrLogger类
public void log(String msg) {
System.err.println(msg);
}
FileLogger类
public void log(String msg) {
// Construct the timestamp we will use, if requested
Timestamp ts = new Timestamp(System.currentTimeMillis());
//ts.toString()返回的日期格式如下:yyyy-mm-dd hh:mm:ss.fffffffff
//fffffffff表示纳秒,substring(0,19)去除掉后面纳秒数
String tsString = ts.toString().substring(0, 19);
//以天为单位
String tsDate = tsString.substring(0, 10);
// If the date has changed, switch log files
//如果day发生了变化,换一个文件重新记录
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);
}
}
}
private void close() {
if (writer == null)
return;
writer.flush();
writer.close();
writer = null;
date = "";
}
private void open() {
// Create the directory if necessary
File dir = new File(directory);
if (!dir.isAbsolute())
dir = new File(System.getProperty("catalina.base"), directory);
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;
}
}
直接看看Bootstrap即可
// ------ add logger --------
System.setProperty("catalina.base", "D:\\tomcat\\HowTomcatWorks");
FileLogger logger = new FileLogger();
logger.setPrefix("FileLog_");
logger.setSuffix(".txt");
logger.setTimestamp(true);
logger.setDirectory("webroot");
context.setLogger(logger);
How tomcat works(深入剖析tomcat)(7) Logger
标签:cat print files value open() work 图片 变化 概述
原文地址:https://www.cnblogs.com/danzZ/p/13999042.html