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 "%r" %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,"%r",%s,%b,%{Referer}i,"%{User-Agent}i",%{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,"%r",%s,%b,%{Referer}i,"%{User-Agent}i",%{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 }