筛选日志工具

用途:埋点测试

实现功能:查询较小日志文件,筛选key值出现次数及对应行日志

package com.ni.gm;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class service {
    private String key;                      //输入筛选key值
    int outCount =0;                         //输出点击上报次数
    public service(String key){
        this.key = key;
    }

    public void selectLog(String filePath) throws Exception{
        //复制文件
        //打开copy文件
        FileInputStream logFile = new FileInputStream(filePath);

        //读取文件
        try {
            InputStreamReader isr = new InputStreamReader(logFile, "utf8");
            BufferedReader br = new BufferedReader(isr);

            String line;
            while ((line = br.readLine()) != null) {
             
                //正则匹配key
                Pattern pattern = Pattern.compile(key,Pattern.CASE_INSENSITIVE); //Pattern.CASE_INSENSITIVE参数控制正则不区分大小写,如果完全匹配去掉该参数
                Matcher matcher = pattern.matcher(line);
                while(matcher.find()){
                    //String countline = matcher.group(0);   //GROUP(0)全量匹配
                    outCount +=1;
                    System.out.println("筛选日志:" + line);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //输出,点击上报次数
        System.out.println("筛选次数:"+ outCount);

        //输出页面停留时间(待补充)

    }
    public String copyFile(String srcPath, String targetPath) throws IOException {
        File srcFile = new File(srcPath);
        File targetFile = new File(targetPath);
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(targetFile);
        byte[] buf = new byte[8 * 1024];
        int len ;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        in.close();
        out.flush();
        out.close();
        return targetPath;

    }
   
}

主函数

package com.ni.gm;

import org.testng.annotations.Test;

@Test
public class AppTest
{
    service se = new service("userdetailpage_nav_item_click");
    @Test
    public void shouldAnswerWithTrue() throws Exception
    {
    
        String filePath = se.copyFile("D:\\log.txt","D:\\log1.txt");
        se.selectLog(filePath);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值