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

本文介绍了一种Java项目中处理异常并记录日志的方法。通过遍历项目中的Java文件,查找特定关键字,并在相应的catch块中添加日志记录语句。此方法有助于区分业务异常和常规异常。

需求:项目里面的日志不能够打印出异常栈

分析:项目里面的异常分为两种异常,一种是业务异常比如登录次数过多,比如密码不对等等,约定这样的异常不用处理。所以我们只需要打印出特征码不太明显的常规异常即可,废话少说直接上代码

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);
	}
}

分为这么几步

1.获取项目当中所有的java文件

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

3.在下面一行打印出日志即可。

 

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

转载于:https://my.oschina.net/u/1455908/blog/865596

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值