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     }

 

转载于:https://www.cnblogs.com/wang-yaz/p/10030357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值