1. 程式人生 > >spring boot Tomcat訪問日誌

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     }