批量增长日志打印的java程序

需求:项目里面的日志不可以打印出异常栈java

分析:项目里面的异常分为两种异常,一种是业务异常好比登陆次数过多,好比密码不对等等,约定这样的异常不用处理。因此咱们只须要打印出特征码不太明显的常规异常便可,废话少说直接上代码测试

public class FileInsertRow {
	public static void main(String args[]) {
		try {
			List<String> list = new ArrayList<String>();
			File pathFiles = new File("D:\\log\\files.text");
			BufferedReader reader = null;
			try {
				String tempString = null;
				reader = new BufferedReader(new FileReader(pathFiles));
				while ((tempString = reader.readLine()) != null) {
					list.add(tempString);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e1) {
					}
				}
			}
			FileInsertRow test = new FileInsertRow();
			for (String pathName : list) {
				System.out.println("处理完成的文件数"+pathName);
				List<Integer> listNumbers = readFileByLines(pathName);
				File srcFile = new File(pathName);// 首先存在文件,文件内容:1
				if(!listNumbers.isEmpty()){
					System.out.println("驾驶"+pathName);
					test.insertStringInFile(srcFile,listNumbers, "			log.info(\"cause异常信息上:\",e);");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void traverseFolder2(String path,List<String> list) {

        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
//                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath(),list);
                    } else {
                       	if(file2.getName().endsWith("Module.java")){
                       		list.add(file2.getAbsolutePath());
                    	}
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
	
	 /**
     * 以行为单位读取文件,经常使用于读面向行的格式化文件
	 * @throws Exception 
     */
    public static  List<Integer> readFileByLines(String fileName) throws Exception {
    	List<Integer> lineNumbers = new ArrayList<Integer>();
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
            	if(tempString.contains("catch")&&tempString.contains("Exception")){
            		//获取括弧里面的字符串
            		int left =tempString.indexOf("(");
            		int right = tempString.indexOf(")");
            		String exinfo = tempString.substring(left+1, right);
//            		System.out.println("***"+exinfo+"****");
            		String[] execptionstrings = exinfo.split(" ");
            		if(execptionstrings[0].equals("Exception")){
            			lineNumbers.add(line);
            		}
            	}
            	line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                	e1.printStackTrace();
                }
            }
        }
        return lineNumbers;
    }
    public void insertStringInFile(File inFile, List<Integer> linenos, String lineToBeInserted) throws Exception {
		// 临时文件
		File outFile = File.createTempFile("name", ".tmp");
		// 输入
		FileInputStream fis = new FileInputStream(inFile);
		BufferedReader in = new BufferedReader(new InputStreamReader(fis));
		// 输出
		FileOutputStream fos = new FileOutputStream(outFile);
		PrintWriter out = new PrintWriter(fos);
		// 保存一行数据
		String thisLine;
		// 行号从1开始
		int i = 1;
		while ((thisLine = in.readLine()) != null) {
			// 若是行号等于目标行,则输出要插入的数据
			for (int lineno : linenos) {
				if (i == (lineno+1) ){
					System.out.println("插入日志"+lineToBeInserted);
					out.println(lineToBeInserted);
				}
			}
			// 输出读取到的数据
			out.println(thisLine);
			// 行号增长
			i++;
		}
		out.flush();
		out.close();
		in.close();
		// 删除原始文件
		inFile.delete();
		// 把临时文件更名为原文件名
		outFile.renameTo(inFile);
		TimeUnit.SECONDS.sleep(10);
	}
}

分为这么几步this

1.获取项目当中全部的java文件日志

2.遍历该文件查看是否包含hot wordcode

3.在下面一行打印出日志便可。字符串

 

精测试,以上代码可行性极高,在写这段代码的时候,也入了很多坑,好比java文件操做的几个类,读写文件的乱码问题等等。有空再写,可是依着我拖延症的毛病,估计是写不上了````get

相关文章
相关标签/搜索