spring boot Tomcat访问日志

Posted 天道酬勤,厚德载物

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot Tomcat访问日志相关的知识,希望对你有一定的参考价值。

1.Tomcat设置访问日志

 1 <Host name="localhost"  appBase="webapps"
 2             unpackWARs="true" autoDeploy="true">
 3         <!--
 4         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
 5                prefix="localhost_access_log." suffix=".txt"
 6                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
 7         -->
 8         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
 9                prefix="localhost_access_log." suffix=".txt"
10                pattern="%h,%l,%u,%t,%T,&quot;%r&quot;,%s,%b,%{Referer}i,&quot;%{User-Agent}i&quot;,%{X-Requested-With}i" 
11                fileDateFormat="yyyy-MM-dd.HH"
12                />
13 
14       </Host>

2.springboot设置访问日志,在properties配置文件中添加tomcat日志配置

 1 #内嵌tomcat日志
 2 server.tomcat.accesslog.buffered=true
 3 server.tomcat.accesslog.directory=/usr/microStorage/tomcatLog
 4 server.tomcat.accesslog.enabled=true
 5 #每天保存一个
 6 server.tomcat.accesslog.file-date-format=yyyy-MM-dd
 7 server.tomcat.accesslog.pattern=%h %l %T %t %r %s %b %{Referer}i %{User-Agent}i
 8 server.tomcat.accesslog.prefix=access_log
 9 server.tomcat.accesslog.rename-on-rotate=false
10 server.tomcat.accesslog.request-attributes-enabled=false
11 server.tomcat.accesslog.rotate=true
12 server.tomcat.accesslog.suffix=.log

3.日志分析,通过正在表达式,将需要分析的信息进行正则捕获提取,然后根据需要进行相关数据的统计及可视化展示

 1 public static void parse() throws Exception{
 2         String path = "D:/logs/localhost_access_log.2018-09-30.11.txt";
 3         BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
 4         String line = null;
 5         String reg = "^(.+?),(.+?),(.+?),(.+?),(.+?),(\".+?\"),(.+?),(.+?),(.+?),(\".+?\"),(.+?),(.+?)$";
 6         Pattern pattern = Pattern.compile(reg);
 7         while((line=br.readLine())!=null) {
 8             System.out.println(line);
 9             Matcher matcher = pattern.matcher(line);
10             matcher.matches();
11             // pattern="%h,%l,%u,%t,%T,&quot;%r&quot;,%s,%b,%{Referer}i,&quot;%{User-Agent}i&quot;,%{X-Requested-With}i,%{passport}c" 
12             System.out.println("ip="+matcher.group(1));
13             System.out.println("date="+matcher.group(4));
14             System.out.println("cost="+matcher.group(5));
15             System.out.println("request="+matcher.group(6));
16             System.out.println("status="+matcher.group(7));
17             System.out.println("bytes="+matcher.group(8));
18             System.out.println("Referer="+matcher.group(9));
19             System.out.println("User-Agent="+matcher.group(10));
20             System.out.println("X-Requested-With="+matcher.group(11));
21             System.out.println("passport="+matcher.group(12));
22             System.out.println("------------------------------------");
23         }
24         br.close();
25     }

 

以上是关于spring boot Tomcat访问日志的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot中如何配置tomcat访问日志的位置和名称?

带有嵌入式tomcat的Spring Boot +带有身份验证用户的访问日志

你如何告诉 Spring Boot 将嵌入式 Tomcat 的访问日志发送到标准输出?

带有嵌入式tomcat的Spring Boot应用程序,将访问日志发送到syslog

Spring Boot自定义的Tomcat打印catalina和tomcat日志

spring boot .war tomcat 应用程序日志不存在