【原创】处理特殊log语句的java函数

工作中经常需要将log中的某些有用的信息提取出来以供产品或者决策部门使用,自己写了一段小程序来定制这种信息提取动作。

    public static boolean dealWithLog()
    {
        String charsetName="UTF-8";
        String FilePath = "E:\\log\\";
        String FileName = "Idraw.txt";
        String FileOutName = "Idresult.txt"; 
        String separatePattern = "发现来自";
        String pattern_1 = "的恶意用户";
        String pattern_2 = "在进行短信";
        String fullFileName = FilePath+FileName;
        String fullFileOutName = FilePath+FileOutName;
        
        BufferedReader d;
        BufferedWriter w;
        InputStreamReader insReader;
        OutputStreamWriter outWriter;
        String temp;
        String tempOut;
        String tempTem;
        
        int patternLen = separatePattern.length();
        int pattern_1Len = pattern_1.length();
        int pattern_2Len = pattern_2.length();
        
        Map<String, String> idMap = new HashMap<String, String>();
        int patternPos = -1;
        int pattern_1Pos = -1;
        int pattern_2Pos = -1;
        
        int lineIndex = 0;
        
        try
        {
            insReader = new InputStreamReader(new FileInputStream(fullFileName), charsetName);
            outWriter = new OutputStreamWriter(new FileOutputStream(fullFileOutName), charsetName);
            d = new BufferedReader(insReader);
            w = new BufferedWriter(outWriter);
            
            while(((temp = d.readLine()) != null) && (!temp.equalsIgnoreCase("")))
            {
                patternPos = temp.indexOf(separatePattern);
                pattern_1Pos = temp.indexOf(pattern_1);
                pattern_2Pos = temp.indexOf(pattern_2);
                
                if (patternPos != -1 &&
                        pattern_1Pos != -1 &&
                        pattern_2Pos != -1)
                {
                    tempTem = temp.substring(pattern_1Pos+pattern_1Len, pattern_2Pos);
                    System.out.println("--> No "+lineIndex+" line processed <--");
                    System.out.println("tempTem: >"+tempTem+"<");
                    String tmpMapVal = idMap.get(tempTem);
                    if (StringUtils.isEmpty(tmpMapVal))
                    {
                        System.out.println("added tempTem: >"+tempTem+"<");
                        idMap.put(tempTem, "gotIt");
                    }
                }
                
                lineIndex ++;
            }
            
            w.write("【2014-01-07,2014-01-08,2014-01-09获取短信失败的用户ID列表】");
            w.newLine();
            
            //w.flush();
            
            for (String key : idMap.keySet()) {
                w.write(key);
                w.newLine();
                
                w.flush();
            }
            
            w.close();
            d.close();
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }


你可能感兴趣的:(java,log,日志)