1、Tomcat设置访问日志
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h,%l,%u,%t,%T,"%r",%s,%b,%{Referer}i,"%{User-Agent}i",%{X-Requested-With}i" fileDateFormat="yyyy-MM-dd.HH" /> </Host>
在pattern中每个的含义,我参考的是 https://blog.csdn.net/yaerfeng/article/details/40340981
2、springboot设置访问日志,在配置文件中添加tomcat日志配置
#tomcat访问日志
server.tomcat.accesslog.directory=d:/logs
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.prefix=localhost_access_log.
server.tomcat.accesslog.suffix=.txt
server.tomcat.accesslog.pattern=%h,%l,%u,%t,%T,"%r",%s,%b,%{Referer}i,"%{User-Agent}i",%{X-Requested-With}i,%{passport}c
server.tomcat.accesslog.file-date-format=yyyy-MM-dd
我是添加到application.properties中的
3、日志分析,通过正在表达式,将需要分析的信息进行正则捕获提取,然后根据需要进行相关数据的统计及可视化展示
public static void parse() throws Exception{ String path = "D:/logs/localhost_access_log.2018-09-30.11.txt"; BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String line = null; String reg = "^(.+?),(.+?),(.+?),(.+?),(.+?),(\".+?\"),(.+?),(.+?),(.+?),(\".+?\"),(.+?),(.+?)$"; Pattern pattern = Pattern.compile(reg); while((line=br.readLine())!=null) { System.out.println(line); Matcher matcher = pattern.matcher(line); matcher.matches(); // pattern="%h,%l,%u,%t,%T,"%r",%s,%b,%{Referer}i,"%{User-Agent}i",%{X-Requested-With}i,%{passport}c" System.out.println("ip="+matcher.group(1)); System.out.println("date="+matcher.group(4)); System.out.println("cost="+matcher.group(5)); System.out.println("request="+matcher.group(6)); System.out.println("status="+matcher.group(7)); System.out.println("bytes="+matcher.group(8)); System.out.println("Referer="+matcher.group(9)); System.out.println("User-Agent="+matcher.group(10)); System.out.println("X-Requested-With="+matcher.group(11)); System.out.println("passport="+matcher.group(12)); System.out.println("------------------------------------"); } br.close(); }