设为首页 收藏本站
查看: 621|回复: 0

[经验分享] 12,tomcat日志处理

[复制链接]

尚未签到

发表于 2017-1-25 13:21:09 | 显示全部楼层 |阅读模式
  日志系统是一个记录信息的组件。在Catalina中,日志系统是一个相对简单的跟容器相关联的组件。
Tomcat在org.apache.catalina.logger包中提供了多个不同的日志系统
Logger接口
一个日志系统必须实现org.apache.catalina.Logger接口

public interface Logger {

// ----------------------------------------------------- Manifest Constants

/**
* Verbosity level constants for log messages that may be filtered
* by the underlying 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;

// ------------------------------------------------------------- Properties

/**
* Return the Container with which this Logger has been associated.
*/
public Container getContainer();

/**
* Set the Container with which this Logger has been associated.
*
* @param container The associated Container
*/
public void setContainer(Container container);

/**
* Return descriptive information about this Logger implementation and
* the corresponding version number, in the format
* <code>&lt;description&gt;/&lt;version&gt;</code>.
*/
public String getInfo();

/**
* Return the verbosity level of this logger.  Messages logged with a
* higher verbosity than this level will be silently ignored.
*/
public int getVerbosity();

/**
* Set the verbosity level of this logger.  Messages logged with a
* higher verbosity than this level will be silently ignored.
*
* @param verbosity The new verbosity level
*/
public void setVerbosity(int verbosity);

// --------------------------------------------------------- Public Methods

/**
* Add a property change listener to this component.
*
* @param listener The listener to add
*/
public void addPropertyChangeListener(PropertyChangeListener listener);

/**
* Writes the specified message to a servlet log file, usually an event
* log.  The name and type of the servlet log is specific to the
* servlet container.  This message will be logged unconditionally.
*
* @param message A <code>String</code> specifying the message to be
*  written to the log file
*/
public void log(String message);

/**
* Writes the specified exception, and message, to a servlet log file.
* The implementation of this method should call
* <code>log(msg, exception)</code> instead.  This method is deprecated
* in the ServletContext interface, but not deprecated here to avoid
* many useless compiler warnings.  This message will be logged
* unconditionally.
*
* @param exception An <code>Exception</code> to be reported
* @param msg The associated message string
*/
public void log(Exception exception, String msg);

/**
* Writes an explanatory message and a stack trace for a given
* <code>Throwable</code> exception to the servlet log file.  The name
* and type of the servlet log file is specific to the servlet container,
* usually an event log.  This message will be logged unconditionally.
*
* @param message A <code>String</code> that describes the error or
*  exception
* @param throwable The <code>Throwable</code> error or exception
*/
public void log(String message, Throwable throwable);

/**
* Writes the specified message to the servlet log file, usually an event
* log, if the logger is set to a verbosity level equal to or higher than
* the specified value for this message.
*
* @param message A <code>String</code> specifying the message to be
*  written to the log file
* @param verbosity Verbosity level of this message
*/
public void log(String message, int verbosity);

/**
* Writes the specified message and exception to the servlet log file,
* usually an event log, if the logger is set to a verbosity level equal
* to or higher than the specified value for this message.
*
* @param message A <code>String</code> that describes the error or
*  exception
* @param throwable The <code>Throwable</code> error or exception
* @param verbosity Verbosity level of this message
*/
public void log(String message, Throwable throwable, int verbosity);

/**
* Remove a property change listener from this component.
*
* @param listener The listener to remove
*/
public void removePropertyChangeListener(PropertyChangeListener listener);

}
  日志接口提供了日志系统要实现的方法,最简单的方法是接受一个字符串并将其记录,
最后两个方法会接受一个冗余级别(verbosity level),如果传递的数字低于该类的实例设置的冗余级别,就将信息记录下来,否则就忽略信息。
静态变量定义了五个冗余级别:FATAL, ERROR, WARNING, INFORMATION,和 DEBUG。getVerbosity和setVerbosity分别用来获得和设置冗余级别。
日志接口还有getContainer 和 setContainer方法用来将日志系统跟容器关联起来
  Tomcat日志系统
Tomcat提供了三种日志系统,它们分别是FileLogger, SystemErrLogger, 和 SystemOutLogger。
都继承了org.apache.catalina.logger.LoggerBase类
  
DSC0000.png
 
  LoggerBase类
LoggerBase类是一个抽象类,它实现了Logger接口中除log(String msg)之外的所有方法。
 public abstract void log(String msg);
该方法需要在子类进行覆盖(overload),所有的其他的log方法都调用了该方法
SystemOutLogger类
SystemOutLogger作为LoggerBase的子类提供了log(String message)方法的实现
每一个收到的信息都被传递给System.out.println
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类
FileLogger是LoggerBase类中最复杂的
它将从关联容器收到的信息写到文件中,每个信息可以选择性的加上时间戳。
在第一次实例化的时候,该类的实例会创建一个文件,该文件的名字带有日期信息。
如果日期改变了,它会创建一个新的文件并把信息写在里面。

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);
}
}
}
  log方法接受一个消息并把消息写到日志文件中。
在FileLogger实例的生命周期中,log方法可以打开或关闭多个日志文件。
写日志的方法

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;
}
}
  open方法首先应该创建日志的目录是否存在,如果目录不存在,则首先创建目录
Close方法清空PrintWriter变量writer,然后关闭PrintWriter并将writer设置为null
 private void close() {
  if (writer == null)
            return;
        writer.flush();
        writer.close();
        writer = null;
        date = "";
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-333377-1-1.html 上篇帖子: tomcat源码解析--SessionManager 下篇帖子: 12,tomcat日志处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表