• tomcat和springboot访问日志及分析


    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 &quot;%r&quot; %s %b" />
            -->
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log." suffix=".txt"
                   pattern="%h,%l,%u,%t,%T,&quot;%r&quot;,%s,%b,%{Referer}i,&quot;%{User-Agent}i&quot;,%{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,&quot;%r&quot;,%s,%b,%{Referer}i,&quot;%{User-Agent}i&quot;,%{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();
        }
  • 相关阅读:
    数据挖掘实践(45):实战--啤酒产量时序分析(三)
    数据挖掘实践(44):实战--啤酒产量时序分析(二)
    数据挖掘实践(43):实战--啤酒产量时序分析(一)
    数据挖掘实践(42):算法基础(十四)时间序列分析(五)
    Java 之 数组 案例(不重复的数组&回形数)
    Java 之 数组中常见的异常
    Java 之 数组 Arrays 工具类
    Java 之 数组常用算法
    Java 之 多维数组(二维数组)
    Java 之 一维数组
  • 原文地址:https://www.cnblogs.com/TheoryDance/p/9730232.html
Copyright © 2020-2023  润新知