1.1 安装 Tomcat
安装Tomcat的本体和相关官方测试demo,参考链接
apt-get install tomcat8 -y # 安装Tomcat本体 apt-get install tomcat8-docs tomcat8-examples tomcat8-admin -y # 安装测试demo
1.2 Tomcat 启动检查
systemctl start tomcat8 # 启动Tomcat systemctl status tomcat8 netstat -lntup|grep 8080 # 端口测试 lsof -i:8080 # 端口检查得到输出 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 4502 tomcat8 63u IPv6 125026 0t0 TCP *:http-alt (LISTEN)
1.3 查看 Tomcat 日志
启动tomcat之后,使用本地浏览器访问http://localhost:8080/
访问tomcat页面,在页面中点击按钮产生HTTP请求,让tomcat产生日志
tail -f /var/log/tomcat8/localhost_access_log.2021-08-01.txt
打开Tomcat的server.xml
配置文件进行修改,在日志文件中的文末修改如下对应设置
# 编辑配置文件 vim /etc/tomcat8/server.xml #将以下内容替换配置文件中135行对应内容 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".log" pattern="{"client":"%h", "client user":"%l", "authenticated":"%u", "access time":"%t", "method":"%r", "status":"%s", "send bytes":"%b", "Query?string":"%q", "partner":"%{Referer}i", "Agent version":"%{User-Agent}i"}"/> # 查看修改内容 cat -n /etc/tomcat8/server.xml
重新启动tomcat并查看日志,检验是否配置成功,产生新的日志还是需要通过使用浏览器访问8080端口,在Tomcat的demo样例中对tomcat发送请求产生日志。
# 先清空日志 > /var/log/tomcat8/localhost_access_log.2021-08-02.txt # 重新启动Tomcat systemctl restart tomcat8 # 查看日志 root@master:/var/log/tomcat8# tail -f /var/log/tomcat8/localhost_access_log.2021-08-02.log # 查看日志命令 {"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:23:55 +0000]", "method":"GET /examples/servlets/images/return.gif HTTP/1.1", "status":"200", "send bytes":"1231", "Query?string":"", "partner":"http://172.16.255.131:8080/examples/servlets/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"} {"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:23:57 +0000]", "method":"GET /examples/servlets/servlet/RequestParamExample HTTP/1.1", "status":"200", "send bytes":"673", "Query?string":"", "partner":"http://172.16.255.131:8080/examples/servlets/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"} {"client":"172.16.255.1", "client user":"-", "authenticated":"-", "access time":"[02/Aug/2021:02:24:01 +0000]", "method":"GET /host-manager/html HTTP/1.1", "status":"401", "send bytes":"2044", "Query?string":"", "partner":"http://172.16.255.131:8080/", "Agent version":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}
新增Filebeat输入配置,将tomcat日志参照Nginx的Json格式日志采集方式配置如下
vim
小技巧:将连续多行内容复制对应位置使用t
命令,在Normal模式中输入:2,7t11
表示将第二到第七行的内容复制到第十一行开头;将连续多行内容移动对应位置使用m
命令,在Normal模式中输入:2,7m11
表示将第二到第七行的内容移动到第十一行开头
vim
小技巧:在输入内容时要使用到某个文件路径可以是用!
命令然后使用shell命令查看内容,例如查看某个文件的路径可以在Normal模式中输入:!ls /var/log/tomcat8/...
提示
# ================== Filebeat inputs =============== # ------------------------------Tomcat---------------------------------- - type: log enabled: true paths: # - /var/log/tomcat8/localhost_access_log.2021-08-02.log # 为了能够采集所有日期的日志,将文件名中的指定日期改成通配符`*` - /var/log/tomcat8/localhost_access_log.*.log json.keys_under_root: true json.overwrite_keys: true tags: ["tomcat"] # ================================== Outputs =================================== # ---------------------------- Elasticsearch Output ---------------------------- output.elasticsearch: hosts: ["172.16.255.131:9200"] indices: - index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}" when.contains: tags: "access" - index: "nginx-error-%{[agent.version]}-%{+yyyy.MM}" when.contains: tags: "error" # 在输出配置中添加如下索引设置识别tomcat日志,值得注意的时这里不需要再重新编辑template设置,应该pattern配置只在第一次使用时进行匹配识别 - index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM}" when.contains: tags: "tomcat"
配置完成之后,重新启动Filebeat采集Json格式日志
systemctl restart filebeat
查看ES中存储的Tomcat日志是否是Json格式
到此这篇关于ELK收集Tomcat日志的实现的文章就介绍到这了,更多相关ELK Tomcat日志内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章: