通用的日志记录器(java)
2014-04-28 10:46
441 查看
线程安全的java日志记录器
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.atomic.AtomicReference; /** * * @author zhangshuang * */ public class SyncFile { private static String FILENAME =""; private static String FILEDIR=""; private static final AtomicReference<SyncFile> instance = new AtomicReference<SyncFile>(); private String currentDate = SyncFile.getCurrentDate(); private File file = null; private FileWriter fw = null; private BufferedWriter bw = null; private SyncFile(String dir, String name) { SyncFile.FILEDIR = dir; SyncFile.FILENAME = name ; while(dir.endsWith("/")) { dir = dir.substring(0, dir.length() - 1); } mkDir(dir); String filePath = dir + "/" + name + "." + currentDate ; file = new File(filePath); try { if(!file . exists()) { file.createNewFile(); } fw = new FileWriter(file, true); bw = new BufferedWriter(fw); } catch (Exception e) { e.printStackTrace(); } } public static SyncFile getInstance(String dir, String name) { if( instance .get() == null ) { instance .compareAndSet(null, new SyncFile(dir, name)); } return instance.get(); } public static SyncFile getInstance() { if( instance .get() == null ) { instance.compareAndSet(null, new SyncFile(FILEDIR, FILENAME)); } return instance.get(); } public synchronized void write (String line) { openBuffer() ; line = line.endsWith("\n") ? line : line + "\n"; try { if(line != null && line.trim() != ""){ bw.write(line); bw.flush(); } } catch (IOException e) { e.printStackTrace(); } } private static void mkDir( String dir) { File file = new File(dir); mkDir(file); } /** * <p>递归创建不存在的目录 * @param file */ private static void mkDir(File file){ if(file.getParentFile().exists()){ file.mkdir(); }else{ mkDir(file.getParentFile()); file.mkdir(); } } public static String getCurrentDate() { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()); } private void openBuffer() { if(!currentDate.equals(SyncFile.getCurrentDate()) || fw == null ||bw == null) { if (!currentDate.equals(SyncFile.getCurrentDate())){ currentDate = SyncFile.getCurrentDate(); file = new File(FILEDIR + "/" + FILENAME + "." + currentDate); } close (); try { fw = new FileWriter(file , true); bw = new BufferedWriter(fw); } catch (IOException e) { e.printStackTrace(); } } } public void close() { try { if (bw != null) { bw.close(); bw = null; } if (fw != null) { fw.close(); fw = null; } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // String a = "/data/logs/////"; // while (a.endsWith("/")) { // a = a.substring(0 , a.length()-1); // System.out.println(a); // } // System.out.println("ok" + a); mkDir("c:/2"); } }
相关文章推荐
- [转发]在 Java Web 项目中选择使用 Slf4J 通用日志框架
- 通用的日志记录器(PHP)
- JAVA实现通用日志记录方法
- JAVA实现通用日志记录
- JAVA实现通用日志记录
- [转发]在 Java Web 项目中选择使用 Slf4J 通用日志框架
- 一个命令搞定Java Web通用添删改查及分页代码生成(音视频)
- log4j.properties(java日志文件)语法语句解析,什么意思,使用方法举例
- Java GC日志查看
- java基础日志
- 5.Java中的异常、断言、日志【草稿上】
- java 自学日志【二十三】---张老师银行业务调度系统
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- Java Thread Dump 日志分析
- java的几个日志框架log4j、logback、common-logging
- Java学习日志(13-2-StringBuffer/StringBuilder与基本数值类型包装类)
- Java项目中使用log记录日志的一些总结
- Java日志,需要知道的几件事(commons-logging,log4j,slf4j,logback)
- java中的IO整理(3)----数据操作流---合并流--压缩流--输入输出重定向 - 老秋的日志 - 网易博客
- Java游戏开发日志(2007-5-29)