前言:
项目做了一部分,要有持久化日志这一功能。目的是方便以后建库。项目后台预设了访问记录,登录记录等功能,但是一期还没有写到这个地方,所以需要做一些持久化信息的操作,一方面方便记录信息,一方面也要为以后建库提供数据;
因为以后这些数据是要建库的,所以样式什么的也不能随便写。所以也就封装了一个工具类,便于做这些日志信息或者建库之前数据的持久化;
当然了,这个类亦可以用于有顺序的往文件里写入数据,也支持无顺序的写入,也支持自定义的格式写入;
上代码:
package souqu.life.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import com.alibaba.fastjson.JSON; /** * Log数据持久化类 * @author 贾恒飞 * @remark 日志信息的持久化操作类,对于日志的存储操作,内置了一些常用的存储格式,也允许用户自定义存储模式 */ public class LogPersist { private static LogPersist operation;//单例操作工具类 private LogPersist (){} /** * 获取log操作对象 * @return LogPersist */ public static LogPersist getOperation(){ if (operation==null) { operation = new LogPersist(); } return operation; } /** * 获取预定义的log模板类 * @return Log(该类是一个内部类,只能通过此方法获取实例) */ public Log log(){ return new Log(); } /** * 使用自定义的样式,存储log信息 * @param format 规范方案(方案中每一个msg的位置使用"?"代替) * 例:'[?],?,---:?'--->'[2018-4-17],你好,---:在呢!' * @param msg 信息 */ public void log(Format format,String... msg){ String chars = format.logsFormat(); for (String string : msg) { chars = chars.replaceFirst("?", string); } tofiles(format.gainPath(), chars); } /** * Log模板类 * @author 贾恒飞 * @@remark 该类是最终类,无法被重写和继承。该类提供了一些预定的解决方案 */ public final class Log{ /** * 保存log为csv格式 */ public void csv(Path path,String... msg){ String chars = "\n";//数据初始化一个换行符 for (String str : msg) { chars = chars+str+","; } tofiles(path.gainPath(), chars.substring(0,chars.length()-1));//去除最后一个逗号 } /** * 保存log为json格式(每次记录都是一个对象) */ public <T> void json(Path path,T t){ String json = ""; try { json = JSON.toJSONString(t); } catch (Exception e) { e.printStackTrace(); } if(!json.isEmpty()){ tofiles(path.gainPath(), json+",\n"); } } /** * 保存log为通配符格式 */ public void wildcard(String wildcard,Path path,String... msg){ String chars = "\n";//数据初始化一个换行符 for (String str : msg) { chars = chars+str+wildcard; } tofiles(path.gainPath(), chars.substring(0,chars.length()-1));//去除最后一个通配符 } /** * 保存log为纯文本格式,无换行无空格 */ public void txt(Path path,String... msg){ String chars = ""; for (String string : msg) { chars = chars + string; } tofiles(path.gainPath(), chars); } } /** * log信息存储路径 * @author 贾恒飞 */ public interface Path{ String gainPath(); } /** * 信息存储模板 * @author 贾恒飞 */ public interface Format extends Path{ String logsFormat(); } /** * 默认路径 * @param fileName 文件名 * @return 返回一个路径 */ public String path(String fileName){ return "C://logs/"+fileName+".txt"; } /** * 向指定文件追加写入内容 * @param filePath 文件路径(如果文件存在,则追加内容;如果文件不存在,则创建文件) * @param msg 追加的内容 */ private void tofiles(String filePath,String msg) { FileWriter fw = null; try { // 如果文件存在,则追加内容;如果文件不存在,则创建文件 File f = new File(filePath); fw = new FileWriter(f, true); } catch (IOException e) { e.printStackTrace(); } PrintWriter pw = new PrintWriter(fw); pw.println(msg); pw.flush(); try { fw.flush(); pw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } } |
默认提供了四种模版的数据,分别是csv表格格式,json格式,通配符格式和纯文本模式。这四种模式其实也可以满足大部分的需求了,如果需要定制一些样式,可以在页面实现Format接口,调用log方法传入Format接口即可;