造轮子之自己写log文件

今天读了一篇关于java里日志的博客,提到如何写日志的问题,里面有日志的级别,日志的内容,写到了哪里,实际上自己也可以写日志,而且自己定义的日志内容比较个性化,日志最终的目的就是为了自己能够看懂,为报错提供可靠的查找依据。

所以生成日志的思路很简单,生成一个以今天日期为名字的文件,然后写入,操作名称,操作内容,和操作时间即可,这种情况适用于单个客户端产生的日志保存在本地,因为这个方法会一直对该文件进行写入,知道今天过去,开始明天的日志才会新建另一个日志文件。

首先是创建文件,如果文件存在,则不操作,如果文件不存在,则进行创建

public static boolean createFile(String destFileName) {
		File file = new File(destFileName);
		if (file.exists()) {
			return false;
		}
		if (destFileName.endsWith(File.separator)) {
			return false;
		}
		// 判断目标文件所在的目录是否存在
		if (!file.getParentFile().exists()) {
			// 如果目标文件所在的目录不存在,则创建父目录
			if (!file.getParentFile().mkdirs()) {
				return false;
			}
		}
		// 创建目标文件
		try {
			if (file.createNewFile()) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

写入文件内容,而且是追加,不能覆盖之前的文件内容,而且要换行,方便阅读(如果不为了阅读,可以不换行)

public static void WriteStringToFile(String filePath,String tag,String message) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");// 设置日期格式
			String dateDfName = df.format(new Date());// new Date()为获取当前系统时间
            FileWriter fw = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("\r\n"+dateDfName+SIGN+tag+SIGN+message);// 往已有的文件上添加字符串
            bw.close();
            fw.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

上面代码定义了tag和message,两个参数,举个例子比较好解释,例如:10001操作表示向数据库插入一条记录,插入内容为“张三,男,18944343434,42423423423432”,10001对应tag,姓名、性别、手机号、身份证对应message,组合起来表示在某个时间发起了10001交易,对数据进行插入操作内容如上面所示。

完整代码如下

package com.caeser.createfile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;

public class CreateFileUtil {
	private static String SIGN = ">>";
	private static String realPath = "D:/hrsys/log/";
	private static String extendName = ".log";

	public static boolean createFile(String destFileName) {
		File file = new File(destFileName);
		if (file.exists()) {
			return false;
		}
		if (destFileName.endsWith(File.separator)) {
			return false;
		}
		// 判断目标文件所在的目录是否存在
		if (!file.getParentFile().exists()) {
			// 如果目标文件所在的目录不存在,则创建父目录
			if (!file.getParentFile().mkdirs()) {
				return false;
			}
		}
		// 创建目标文件
		try {
			if (file.createNewFile()) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	public static void WriteStringToFile(String filePath,String tag,String message) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");// 设置日期格式
			String dateDfName = df.format(new Date());// new Date()为获取当前系统时间
            FileWriter fw = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("\r\n"+dateDfName+SIGN+tag+SIGN+message);// 往已有的文件上添加字符串
            bw.close();
            fw.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	public static void WriteLog(String tag,String message) {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
		String dateName = df.format(new Date());// new Date()为获取当前系统时间
		String finalFileNameString=realPath+dateName+extendName;
		createFile(finalFileNameString);
		WriteStringToFile(finalFileNameString,tag,message);
	}

	public static void main(String[] args) {
		WriteLog("10001", "操作数据库");
	}
}

猜你喜欢

转载自blog.csdn.net/Caeser110/article/details/88384313