自己用java写了一款日志查找分析工具

工作中需要查日志,在日志中发现grep不能把一个时间戳中的内容都搜索到,因为grep只能按行读文件,于是自己写了一款查日志的工具,可以实现多文本、多正则表达式匹配文本并打印。源代码分享如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findtextinlog;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Lion
 */
public class FindTextInLog {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        double startTime = System.currentTimeMillis();
        
        // TODO code application logic here
        FileUtil myFile = new FileUtil("C:\\test.log","[2]\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d");
        String myString = "保存需求上报流程数据";
        String myString2 = "计通[2015]66号《关于下发《基于全过程的管线资源建设管理要求》的通知》";
        
        ArrayList findRegexTextResult = new ArrayList();
        
        ArrayList<LineItem> myList = myFile.getItemArrayList();
        
        for(LineItem a:myList)
        {
            String tmp = a.getTextString().toString();
            
            if(StringUtil.findTextIgnoreCase(tmp, myString) && StringUtil.findTextIgnoreCase(tmp, myString2))
            {
                System.out.println(tmp);
            }
            
        }
        
        double endTime = System.currentTimeMillis();
        System.out.println("The program running: "+(endTime-startTime)/1000);
        
    }
    
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findtextinlog;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Lion
 */
public class StringUtil {
    //定义要找到的关键字
    public String text;
    
    //定义要找到的规则
    
    
    public StringUtil(String aString){
        text = aString;
    }
    
    
    public static boolean findTextIgnoreCase(String sourceString, String text){
        String ignoreCaseString = sourceString.toLowerCase();
        String ignoreText = text.toLowerCase();
        
        CharSequence textSequence;
        textSequence = ignoreText.subSequence(0,ignoreText.length());
        
        return ignoreCaseString.contains(textSequence);    
    }
    
    public static boolean findTextNotIgnoreCase(String sourceString, String text){
        CharSequence textSequence = text.subSequence(0, text.length());
        return sourceString.contains(textSequence);
    }
    
    public static boolean findPattern(String sourceString, String myParttern){
        Pattern pattern = Pattern.compile(myParttern);
        Matcher matcher;
        matcher = pattern.matcher(sourceString);
        return matcher.find();
    }
    
    public static String getPatternString(String sourceString , String myPattern){
        Pattern pattern = Pattern.compile(myPattern);
        Matcher matcher;
        matcher = pattern.matcher(sourceString);
        if(matcher.find()){
            return matcher.group();
        }
        else
            return null;
    }
    
//    public static void main(String[] args){
//        String a = "我爱北京天安门";
//        String b = "北京a";
//        
//        String c = "abcdefghigk";
//        String d = "K";
//        String e = "x ";
//        System.out.println("aaaaaaa");
//        System.out.println(findTextIgnoreCase(a,b));
//        System.out.println(findTextNotIgnoreCase(c,d));
//        
//        String xx = "我爱北京天安门*************";
//        
//        String abc = "2015-11-20 11:26:24,434 [WebContainer : 13] INFO  com.mocha.bpm.hncmcc.itxq.action"
//                + ".ReportingProcessAction:"
//                + "356  - 保存需求上报流程数据";
//        
//        String part ="[2]\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d";
//        
//        System.out.println(findPattern(abc,part));
//        System.out.println(getPatternString(abc,part));
//        
//        ArrayList mylist = new ArrayList();
//        mylist.add(1);
//        mylist.add(2);
//        System.out.println(mylist.size());
//        System.out.println(mylist.get(mylist.size()-1));
//        
//        try {
//         
//            
//            InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("C:\\test.log")), "gb2312"); 
//            BufferedReader myB = new BufferedReader(isr);
//            String aa = myB.readLine();
//            System.out.println(aa);
//            myB.close();
//            isr.close();
//            
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(StringUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
//    }
    
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findtextinlog;

/**
 *
 * @author Lion
 */
public class LineItem {
    public String date;
    public StringBuffer textStringB;
    
    public String getDate(){
        return date;
    }
    
    public StringBuffer getTextString(){
        return textStringB;
    }
    
    public LineItem(String date, String textString){
        this.date = date;
        StringBuffer myStringBuffer = new StringBuffer(textString);
        this.textStringB = myStringBuffer;
    }
    
    public void appendString(String text){
        this.textStringB.append(text);
    }
    
}

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package findtextinlog;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lion
 */
public class FileUtil {
    
    private ArrayList<LineItem> itemArrayList;
    
    public String textURL;
    
    public FileUtil(String url, String regex){
        textURL = url;
        try {
            itemArrayList = new ArrayList();
            
            //这里比较重要,通过这样的转换消除了中文乱码问题
            InputStreamReader myInputStreamReader = new InputStreamReader(new FileInputStream(url),"gb2312");
       
            BufferedReader myBufferedReader = new BufferedReader(myInputStreamReader);
            
            String valueString = null;
                while ((valueString = myBufferedReader.readLine())!=null){
                    //System.out.println("* "+valueString);
                    if(StringUtil.findPattern(valueString, regex)){
                        String date = StringUtil.getPatternString(valueString, regex);
                        itemArrayList.add(new LineItem(date, valueString));
                    }
                    else if(itemArrayList.isEmpty()){
                        itemArrayList.add(new LineItem("",valueString));
                    }
                    else{
                        int size = itemArrayList.size();
                        itemArrayList.get(size-1).appendString(valueString);
                    }
                }
                myBufferedReader.close();
               myInputStreamReader.close();
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ArrayList getItemArrayList(){
        return this.itemArrayList;
    }
    
    
    
}



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值