收集 Tomcat 日志

安装 Tomcat

# 安装 jdk
[root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm # 下载
[root@web01 ~]# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.0-M7/bin/apache-tomcat-10.0.0-M7.tar.gz # 解压
[root@web01 ~]# tar xf apache-tomcat-10.0.0-M7.tar.gz -C /usr/local/ # 做软连接
[root@web01 ~]# ln -s /usr/local/apache-tomcat-10.0.0-M7 /usr/local/tomcat # 启动 Tomcat
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh # 6.访问页面 10.0.0.7:8080

收集 Tomcat 访问日志(Access-log)

Tomcat 访问日志(Access-log)的格式在 server.xml 中可以直接修改,先修改访问格式的日志试试水 ~ ~ ~

# 把原来的日志格式注释,添加我们的格式
[root@web01 ~]# vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".log"
pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/> # 重启 tomcat
[root@web01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@web01 ~]# /usr/local/tomcat/bin/startup.sh # 配置收集新的 tomcat 日志
[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
file {
path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "tomcat_json_%{+YYYY-MM-dd}"
}
}

收集 Tomcat 服务运行日志(Catalina-log)

一般我们会收集 Tomcat 服务运行日志(Catalina-log),当遇到报错时,一条报错会被分割成很多条数据,不方便查看,解决方法:

①. — 修改 Tomcat 的 Catalina 日志格式为 Json

​ — 1)开发修改输出日志为 Json

​ — 2)修改 Tomcat 配置,日志格式为 Json

②. — 使用 Logstash 的 input 插件下的 mutiline 模块

下面使用第二种方法,即 mutiline 模块实现服务运行日志的切分

Mutiline 模块初识

# 使用 mutiline 模块
[root@web01 ~]# vim /etc/logstash/conf.d/test_mutiline.conf
input {
stdin {
codec => multiline {
# 匹配以 `[` 开头
pattern => "^\["
# 匹配到
negate => true
# 向上合并,向下合并是 next
what => "previous"
}
}
}
output {
stdout {
codec => "json"
}
} # 测试,输入内容不会直接输出,当遇到以 [ 开头才会收集以上的日志
[root@web01 ~]# logstash -f /etc/logstash/conf.d/test_mutiline.conf
......
...
]
A
B
C
D
[
{"tags":["multiline"],"host":"web01","@timestamp":"2020-08-14T02:30:33.906Z","message":"[\n]\nA\nB\nC\nD","@version":"1"}
a
b
c
d
e
f
g
[
{"tags":["multiline"],"host":"web01","@timestamp":"2020-08-14T02:28:58.590Z","message":"[\na\nb\nc\nd\ne\nf\ng","@version":"1"}

收集 Tomcat 错误日志(Catalina-log)

# 收集 catalina 错误日志
[root@web01 conf.d]# vim /etc/logstash/conf.d/catalina_out.conf
input {
file {
path => "/usr/local/tomcat/logs/catalina.*.log"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "catalina_%{+YYYY-MM-dd}"
codec => "json"
}
}

追加 Tomcat 错误日志(Catalina-log)

# 测试,手动添加一些错误的日志,到 Catlina 日志中
[root@web01 ~]# cat tomcat_error.log >> /usr/local/tomcat/logs/catalina.2020-08-14.log

收集 Nginx 日志

安装 Nginx

[root@web01 ~]# yum install -y nginx

收集 Nginx 访问日志(Access-log)

# 配置 nginx 访问日志格式
[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
http {
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}'; access_log /var/log/nginx/access.log json;
... ... # 配置收集访问日志
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "nginx_json_%{+YYYY-MM-dd}"
}
}

收集 Nginx 错务日志(Error-log)

Nginx 中的错误日志不会像 Tomcat 中的错误日志一样分成很多行,只需要正常的配置,按行切分

将获取日志参数分离(方法一)

拆分 message 字段

[root@web01 ~]# vim /etc/logstash/conf.d/tomcat_json_es.conf
input {
file {
path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
start_position => "beginning"
}
} # 把收集到的数据进行处理
filter {
json {
# 将 message 字段中的键值对,拆分成为索引文档中的字段
source => "message"
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "tomcat_json_%{+YYYY-MM-dd}"
}
}

移除 message 字段

因为将 Message 字段拆分到文档字段后,就不需要 Message 字段数据了,所以需要将 Message 字段移除:

# message 数据已经拆分,数据还在,去掉 message 数据
filter {
json {
source => "message"
remove_field => ["message"]
}
}

将获取日志参数分离(方法二)

# 最方便的方法,只需要一行 codec => "json"
# file 中的日志格式一定要是 Json 格式
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_json.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
}
output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "nginx_json_%{+YYYY-MM-dd}"
}
}

综合 Logstash 配置

编辑 Logstash 配置文件

# 收集 catalina 服务启动(及报错)日志
[root@web01 conf.d]# cat catalina_out.conf
input {
file {
path => "/usr/local/tomcat/logs/catalina.*.log"
start_position => "beginning"
codec => multiline {
pattern => "^\["
negate => true
what => "previous"
}
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "catalina_%{+YYYY-MM-dd}"
codec => "json"
}
} # 收集 nginx 、tomcat 访问日志
[root@web01 conf.d]# cat nginx_tomcat.conf
input {
file {
type => "nginx_log"
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
file {
type => "tomcat_log"
path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
start_position => "beginning"
codec => "json"
}
} output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "%{type}_%{+YYYY-MM-dd}"
}
}

启动 Logstash 多实例

[root@web01 conf.d]# logstash -f /etc/logstash/conf.d/nginx_tomcat.conf  &
[root@web01 conf.d]# logstash -f /etc/logstash/conf.d/catalina_out.conf --path.data=/data/logstash/catalina/ &

收集日志到 Redis

安装 Redis(略)

配置输出数据到 Redis

[root@web01 ~]# vim /etc/logstash/conf.d/nginx_to_redis.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
}
output {
redis {
host => "172.16.1.121"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}

Logstash 日志收集(补)的更多相关文章

  1. es redis logstash 日志收集系统排错

    用logstash收集日志并发送到redis,然后通过logstash取redis数据写入到es集群,最近kibana显示日志总是中断,日志收集不过来,客户端重启发现报错: Failed to sen ...

  2. springmvc项目 logback.xml配置 logstash日志收集

    配置logback,需要一个转接的Appender,可以通过Maven依赖加到项目中: <dependency> <groupId>com.cwbase</groupId ...

  3. ELK Stack 介绍 & Logstash 日志收集

    ELK Stack 组成 Software Description Function E:Elasticsearch Java 程序 存储,查询日志 L:Logstash Java 程序 收集.过滤日 ...

  4. StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程)

    @ 目录 StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程) 一.下载ELK的安装包上传并解压 1.Elasticsearch下载 2.Logstash下载 3.Kibana ...

  5. 用ElasticSearch,LogStash,Kibana搭建实时日志收集系统

    用ElasticSearch,LogStash,Kibana搭建实时日志收集系统 介绍 这套系统,logstash负责收集处理日志文件内容存储到elasticsearch搜索引擎数据库中.kibana ...

  6. ELK日志收集分析系统配置

    ELK是日志收益与分析的利器. 1.elasticsearch集群搭建 略 2.logstash日志收集 我这里的实现分如下2步,中间用redis队列做缓冲,可以有效的避免es压力过大: 1.n个ag ...

  7. ELK分布式日志收集搭建和使用

    大型系统分布式日志采集系统ELK全框架 SpringBootSecurity1.传统系统日志收集的问题2.Logstash操作工作原理3.分布式日志收集ELK原理4.Elasticsearch+Log ...

  8. 日志收集系统ELK搭建

    一.ELK简介 在传统项目中,如果在生产环境中,有多台不同的服务器集群,如果生产环境需要通过日志定位项目的Bug的话,需要在每台节点上使用传统的命令方式查询,这样效率非常低下.因此我们需要集中化的管理 ...

  9. ELK 日志收集系统

    传统系统日志收集的问题 在传统项目中,如果在生产环境中,有多台不同的服务器集群,如果生产环境需要通过日志定位项目的Bug的话,需要在每台节点上使用传统的命令方式查询,这样效率非常底下. 通常,日志被分 ...

随机推荐

  1. poj-DNA排序

    描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A ...

  2. PHP反序列化 - Pikachu

    概述 序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象: class S{ public $test="pikachu"; } $s ...

  3. paramiko模块简单用法

    最简单最基本的用法 1 #__*__coding:utf-8__*__ 2 import paramiko 3 hostname = '192.168.1.1' 4 username = 'root' ...

  4. oracle编译表上失效USERDBY脚本

    对表进行DLL操作之后,依赖这个表的一些存储过程,触发器等会失效,可以用下边的脚本进行重编译 /* Formatted on 2020/7/8 上午 09:31:31 (QP5 v5.163.1008 ...

  5. REUSE_ALV_GRID_DISPLAY_LVC 的fieldcat定义

    在使用REUSE_ALV_GRID_DISPLAY_LVC函数的时候,需要注意的是,内表中如果有P类型的或者数据元素为BDMNG等类型是,在定义fieldcat的时候,注意要指定fieldcat-da ...

  6. BDC应用

    第一步:SHDB或者是SM35进入BDC录制事务.开始录制. 第二部:保存录制的记录. 第三步:在你自己的程序中定义一个内表如:ITAB TYPE TABLE OF BDCDATA. 再定义一个工作空 ...

  7. spring data JPA 使用EntityentiListeners实现数据审计功能设计

    当系统中有审计需求时,特别是需要对某些数据进行动态监控时,我们可以使用EntityentiListeners来实现,当然这是基于使用JPA而不是mybatis的情况下. 当前我们的需求场景: 1.需要 ...

  8. Xamarin.Forms: 无限滚动的ListView(懒加载方式)

    说明 在本博客中,学习如何在Xamarin.Forms应用程序中设计一个可扩展的无限滚动的ListView.这个无限滚动函数在默认的Xamarin.Forms不存在,因此我们需要为此添加插件.在这里我 ...

  9. VirtualBox Guest Additions 下载地址以及简介

    下载者可将以下链接粘贴到浏览器上,根据Vbox的版本找到自己对应的增强. http://download.virtualbox.org/virtualbox/5.0.10/ 虚拟机安装VBoxAddi ...

  10. Git恢复之前版本的两种方法reset、revert

    实战 回退 1.删除之前的提交 git reset --hard id 推送到远程 git push -f [git log中确实删除了,但是拿到可以恢复] 2.不删除之前的提交 git revert ...