How Tomcat Works(十)
本文接下来分析tomcat的日志记录器,日志记录器是用来记录消息的组件,在tomcat中,日志记录器需要与某个servlet容器相关连;在org.apache.catalina.logger包下,tomcat提供了几种不同类型的日志记录器。
tomcat中的日志记录器都必须实现org.apache.catalina.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(); 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);
}
通常,当传入的日志级别verbosity大于Logger被设置的级别时,message会被记录,否则被忽略
Logger接口共定义了五种日志级别,分别为FATAL ERROR WARNING INFORMATION DEBUG
Tomcat提供了三种类型的日志记录器,分别为SystemOutLogger、SystemErrLogger、FileLogger,在org.apache.catalina.logger包下,均继承自org.apache.catalina.logger.LoggerBase类
LoggerBase类实现了org.apache.catalina.Logger接口,在tomcat5中还实现了Lifecycle接口
LoggerBase类为抽象类,实现了Logger接口中除abstract void log(String msg)外的全部方法实现,所有的其他重载方法最终都会调用该方法(templet模式),我们还可以调用setVerbosity()方法设置日志级别
具体日志类都比较简单,下面是SystemOutLogger类的实现
public class SystemOutLogger
extends LoggerBase { protected static final String info =
"org.apache.catalina.logger.SystemOutLogger/1.0"; public void log(String msg) { System.out.println(msg); }
}
SystemErrLogger类
public class SystemErrLogger
extends LoggerBase { protected static final String info =
"org.apache.catalina.logger.SystemErrLogger/1.0"; public void log(String msg) { System.err.println(msg);
}
}
FileLogger类稍微复杂一点,将从servlet容器中接收到的消息写入到一个文件中(在tomcat4中,FileLogger类实现了Lifecycle接口)
public class FileLogger
extends LoggerBase
implements Lifecycle {
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
private StringManager sm =
StringManager.getManager(Constants.Package); private boolean started = false; private String suffix = ".log"; private boolean timestamp = false; private PrintWriter writer = null;
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); // 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);
}
} } private void close() { if (writer == null)
return;
writer.flush();
writer.close();
writer = null;
date = "";
} /**
* Open the new log file for the date specified by <code>date</code>.
*/
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;
} } public void addLifecycleListener(LifecycleListener listener) { lifecycle.addLifecycleListener(listener);
}
public LifecycleListener[] findLifecycleListeners() { return lifecycle.findLifecycleListeners();
} public void removeLifecycleListener(LifecycleListener listener) { lifecycle.removeLifecycleListener(listener);
} public void start() throws LifecycleException { // Validate and update our current component state
if (started)
throw new LifecycleException
(sm.getString("fileLogger.alreadyStarted"));
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;
} public void stop() throws LifecycleException { // Validate and update our current component state
if (!started)
throw new LifecycleException
(sm.getString("fileLogger.notStarted"));
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
started = false;
close();
} }
---------------------------------------------------------------------------
本系列How Tomcat Works系本人原创
转载请注明出处 博客园 刺猬的温驯
本人邮箱: chenying998179#163.com (#改为@)
本文链接http://www.cnblogs.com/chenying99/p/3237388.html
How Tomcat Works(十)的更多相关文章
- How Tomcat Works(十四)补充
在How Tomcat Works(十四)中,本人并没有对javax.servlet.Filter及javax.servlet.FilterChain做详细的描述,本文在这里做一下补充 FilterC ...
- How Tomcat Works(十四)
我们已经知道,在tomcat中有四种类型的servlet容器,分别为Engine.Host.Context 和Wrapper,本文接下来对tomcat中Wrapper接口的标准实现进行说明. 对于每个 ...
- How Tomcat Works(二十)
要使用一个web应用程序,必须要将表示该应用程序的Context实例部署到一个host实例中.在tomcat中,context实例可以用war文件的形式来部署,也可以将整个web应用拷贝到Tomcat ...
- How Tomcat Works(十八)
在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...
- How Tomcat Works(十六)
本文接下来会介绍Host容器和Engine容器,在tomcat的实际部署中,总是会使用一个Host容器:本文介绍Host接口和Engine接口及其相关类 Host容器是org.apache.catal ...
- How Tomcat Works(十五)
本文接下来分析Context容器,Context容器实例表示一个具体的Web应用程序,其中包括一个或多个Wrapper实例:不过Context容器还需要其他的组件支持,典型的如载入器和Session管 ...
- How Tomcat Works(十二)
tomcat容器通过一个称为Session管理器的组件来管理建立的Session对象,该组件由org.apache.catalina.Manager接口表示:Session管理器必须与一个Contex ...
- How Tomcat Works(十九)
本文重点关注启动tomcat时会用到的两个类,分别为Catalina类和Bootstrap类,它们都位于org.apachae.catalina.startup包下:Catalina类用于启动或关闭S ...
- 攻城狮在路上(肆)How tomcat works(零) 前言说明
最近几篇是关于How tomcat works一书的读书笔记. 通过数个章节逐渐实现一个tomcat的功能. 源码下载地址:http://zhidao.baidu.com/share/7007af0f ...
随机推荐
- Drawable和Bitmap的区别
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一种存储对象比较好 ...
- HTML元素margin、padding的默认值
HTML元素margin.padding的默认值 element margin(单位像素) padding html 0 0 body 8 0 div 0 0 h1 21 0 h2 19 0 19 0 ...
- window+git+AndroidStudio+github
1. 安装配置git 安装:需要从网上下载一个,然后进行默认安装即可.安装完成后,找到 “Git Bash”,点击: 配置: 注意:name和email 只是用来标识身份,但是一定要配置好 2. St ...
- (转载)UITableView的详细讲解
NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量section和row. NSIndexPat ...
- BZOJ 2429 聪明的猴子
kruskal. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- js解决快速回车重复订单提交(客户端方式)
Html代码: <form action="order_add_data.php" method="post" name="order_adds ...
- 【英语】Bingo口语笔记(37) - 动物的多种表达
let the cat out of the bag.不在袋子中的猫 指秘密被泄露 dog tired 累成狗 doggy bag 食品袋
- [转载] ubuntu下定制Vim/Gvim及使用技巧
vim是linux下的编辑器之神,是玩linux的必备工具,同样emacs是神的编辑器,两个编辑器是各有千秋,看个人的喜好,青菜萝卜各有所爱.我是比较喜欢vim,用vim编写bash,perl,pyt ...
- sound tips
ASaudio&SoundAS 两个开源项目阅读: ASaudio&SoundAS 都是比较小巧的声音控制,但似乎都不能直接拿到项目只直接使用. ASaudio ASaudio的Tra ...
- poj 3270(置换群)
题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...