tomcat accesslog日志扩展
由于工作需要,最近对tomcat的日志进行了一些研究,发现其日志大致可以分为两类,一类是运行日志,即平常我们所说的catalina.out日志,由tomcat内部代码调用logger打印出来的;另一类是accesslog访问日志,即记录外部请求访问的信息。处理这两类日志,tomcat默认采用了不同的方式,运行类日志默认采用的是java.util.logging框架,由conf下的logging.properties负责配置管理,也可以支持切换到log4j2(具体可参看我的前一篇博文:升级tomcat7的运行日志框架到log4j2 );对于访问日志,tomcat默认是按日期直接写进文件,由server.xml中配置Valve来管理。
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
此配置会在logs下生成一个localhost_access_log.日期.txt,里面记录每次外部访问的一些信息,信息的内容是根据pattern来配置的,%后加不同的字母表示不同的信息,如上述默认的pattern配置会记录“访问端ip 用户名 时间 第一行请求内容 http状态码 发送字节大小”等内容,详细配置细节可以参考tomcat的accelog(url:https://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging )
@Override
public void log(Request request, Response response, long time) {
if (!getState().isAvailable() || !getEnabled() || logElements == null
|| condition != null
&& null != request.getRequest().getAttribute(condition)
|| conditionIf != null
&& null == request.getRequest().getAttribute(conditionIf)) {
return;
}
/**
* XXX This is a bit silly, but we want to have start and stop time and
* duration consistent. It would be better to keep start and stop
* simply in the request and/or response object and remove time
* (duration) from the interface.
*/
long start = request.getCoyoteRequest().getStartTime();
Date date = getDate(start + time);
// 字符缓冲区
CharArrayWriter result = charArrayWriters.pop();
if (result == null) {
result = new CharArrayWriter(128);
}
// pattern里不同的%表示不同的logElement,此处用result收集所有logElement里追加的内容
for (int i = 0; i < logElements.length; i++) {
logElements[i].addElement(result, date, request, response, time);
}
// 写文件将result写入
log(result);
if (result.size() <= maxLogMessageBufferSize) {
result.reset();
charArrayWriters.push(result);
}
}
其中log(result)实现如下:
@Override
public void log(CharArrayWriter message) {
// 每个一秒检查一下是否需要切换文件
rotate();
// 如果存在文件,先关闭再重新打开一个新日期的文件
if (checkExists) {
synchronized (this) {
if (currentLogFile != null && !currentLogFile.exists()) {
try {
close(false);
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
log.info(sm.getString("accessLogValve.closeFail"), e);
}
/* Make sure date is correct */
dateStamp = fileDateFormatter.format(
new Date(System.currentTimeMillis())); open();
}
}
}
// Log this message 同步加锁写入日志文件,此处使用了buffer
try {
synchronized(this) {
if (writer != null) {
message.writeTo(writer);
writer.println("");
if (!buffered) {
writer.flush();
}
}
}
} catch (IOException ioe) {
log.warn(sm.getString(
"accessLogValve.writeFail", message.toString()), ioe);
}
}
通过上述核心代码可以看到,默认的tomcat是利用缓冲写文件的方式进行访问日志记录的,如果需要分析访问日志,比如找出一天内有多少过ip访问过,或者某一个ip在一分钟内访问了多少次,一般的处理方式是读取accesslog文件内容并进行分析,这么做一方面是无法满足实时分析的目的,更重要的数据量大的时候会严重影响分析效率,因此我们需要对其进行扩展,比如我们可以把访问日志打到kafka或mango中。
- 创建LeKafkaAccesslogValve继承ValveBase并实现AccessLog接口:
@Override
public void log(Request request, Response response, long time) {
if (producerList != null && getEnabled() && getState().isAvailable() && null != this.accessLogElement) {
try {
getNextProducer().send(new ProducerRecord<byte[], byte[]>(topic, this.accessLogElement.buildLog(request,response,time,this).getBytes(StandardCharsets.UTF_8))).get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
log.error("accesslog in kafka exception", e);
}
}
} - 处理可配的参数
private String topic;
private String bootstrapServers; // If set to zero then the producer will not wait for any acknowledgment from the server at all.
private String acks; private String producerSize ; private String properties; private List<Producer<byte[], byte[]>> producerList;
private AtomicInteger producerIndex = new AtomicInteger(0);
private int timeoutMillis;
private boolean enabled = true; // 默认配置问true,即打入kafka,除非有异常情况或主动设置了。 private String pattern;
private AccessLogElement accessLogElement;
private String localeName;
private Locale locale = Locale.getDefault(); - 根据不同的pattern配置解析出需要打印的内容(此部分tomcat8 已经在AbstractAccessLogValve中抽取出来)
public static AccessLogElement parsePattern(String pattern) {
final List<AccessLogElement> list = new ArrayList<>();
boolean replace = false;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < pattern.length(); ++i) {
char ch = pattern.charAt(i);
if (replace) {
if ('{' == ch) {
StringBuilder name = new StringBuilder();
int j = i + 1;
for (; (j < pattern.length()) && ('}' != pattern.charAt(j)); ++j) {
name.append(pattern.charAt(j));
}
if (j + 1 < pattern.length()) {
++j;
list.add(createAccessLogElement(name.toString(), pattern.charAt(j)));
i = j;
} else {
list.add(createAccessLogElement(ch));
}
} else {
list.add(createAccessLogElement(ch));
}
replace = false;
} else if (ch == '%') {
replace = true;
list.add(new StringElement(buf.toString()));
buf = new StringBuilder();
} else {
buf.append(ch);
}
}
if (buf.length() > 0) {
list.add(new StringElement(buf.toString()));
}
return new AccessLogElement() {
@Override
protected String buildLog(Request request, Response response, long time, AccessLog accesslog) {
StringBuilder sBuilder = new StringBuilder(30);
for (AccessLogElement accessLogElement : list) {
sBuilder.append(accessLogElement.buildLog(request, response, time, accesslog));
}
return sBuilder.toString();
}
};
} - 在server.xml中增加配置
<Valve className="com.letv.shop.lekafkavalve.LeKafkaAccesslogValve" enabled="true" topic="info" pattern="%{yyyy-MM-dd HH:mm:ss}t||info||AccessValve||Tomcat||%A||%a||%r||%s||%D" bootstrapServers="kafka地址" producerSize="5" properties="acks=0||producer.size=3"/>tomcat8及以后版本的扩展要方便的多,直接继承AbstractAccessLogValve并重写log方法。

tomcat accesslog日志扩展的更多相关文章
- 配置spring boot 内置tomcat的accessLog日志
#配置内置tomcat的访问日志server.tomcat.accesslog.buffered=trueserver.tomcat.accesslog.directory=/home/hygw/lo ...
- Tomcat AccessLog 格式化
有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式 c-ip s-ip x-InstancePort date time-taken x ...
- spring boot Tomcat访问日志
1.Tomcat设置访问日志 <Host name="localhost" appBase="webapps" unpackWARs="true ...
- Tomcat中日志组件
Tomcat日志组件 AccessLog接口 public interface AccessLog { public void log(Request request, Response respon ...
- Tomcat 修改日志输出配置 定期删除日志
tomcat的下的日志catalina.out 和 qc.log疯狂增长,以下是解决办法 我生产环境tomcat版本 Server version: Apache Tomcat/7.0.35 Serv ...
- Tomcat各种日志的关系与catalina.out文件的分割
Tomcat 各日志之间的关系 一图胜千言! 其他日志如localhost.{yyyy-MM-dd}.log.localhost-access.{yyyy-MM-dd}.log是context的名称, ...
- shell脚本切割tomcat的日志文件
鉴于在调试logback和log4j的文件切割一直无法成功,随性用shell写个脚本用来切割tomcat下的日志文件(大家如果有在logback或log4j使用文件切割成功的话,可以留下使用方式,先谢 ...
- Tomcat访问日志详细配置
在server.xml里的<host>标签下加上 <Valve className="org.apache.catalina.valves.AccessLogValve&q ...
- tomcat详细日志配置
在server.xml里的<host>标签下加上<Valve className="org.apache.catalina.valves.AccessLogValve&qu ...
随机推荐
- Sublime文件夹显示过滤
Preferences/Setting-User添加如下命令: "file_exclude_patterns": ["*.mate", "*.gif& ...
- Minor【 PHP框架】2.第一个应用与请求的生命周期
框架Github地址:github.com/Orlion/Minor (如果觉得还不错给个star哦(^-^)V) 框架作者: Orlion 知乎:https://www.zhihu.com/peop ...
- Bootstrap Metronic 学习记录(一)简介
1.简介 是一个基于Bootstrap 3.x的高级管理控制面板主题.Bootstrap Metronic - 是一个完全响应式管理模板.基于Bootstrap3框架.高度可定制的,易于使用.适合从小 ...
- 【前端性能】必须要掌握的原生JS实现JQuery
很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...
- hdu4833 Best Financing(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4833 这道题目关键的思想是从后往前dp,dp[i]表示在第i处投资xi能获得的最大收益,其中xi表示从 ...
- (转)J2EE的13种核心技术
一.JDBC(Java Database Connectivity) JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问题,另外,JDBC对数据 ...
- HBase 数据模型(Data Model)
HBase Data Model--HBase 数据模型(翻译) 在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的 ...
- Understanding delete
简述 我们都知道无法通过delete关键字针对变量和函数进行操作,而对于显示的对象属性声明却可以进行,这个原因需要深究到js的实现层上去,让我们跟随 Understanding delete 来探究一 ...
- 【JUC】JDK1.8源码分析之Semaphore(六)
一.前言 分析了CountDownLatch源码后,下面接着分析Semaphore的源码.Semaphore称为计数信号量,它允许n个任务同时访问某个资源,可以将信号量看做是在向外分发使用资源的许可证 ...
- 基于 Hive 的文件格式:RCFile 简介及其应用
转载自:https://my.oschina.net/leejun2005/blog/280896 Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度 ...