通用的日志记录器(java)

时间:2014-04-30 02:10:14   收藏:0   阅读:560

线程安全的java日志记录器

 

 

mamicode.com,码迷
  1 import java.io.BufferedWriter;
  2 import java.io.File;
  3 import java.io.FileWriter;
  4 import java.io.IOException;
  5 import java.text.SimpleDateFormat;
  6 import java.util.Date;
  7 import java.util.concurrent.atomic.AtomicReference;
  8 
  9 
 10 
 11 /**
 12  * 
 13  * @author zhangshuang
 14  *
 15  */
 16 public class SyncFile {
 17     private static String FILENAME ="";
 18     
 19     private static String FILEDIR="";
 20     
 21     private static final AtomicReference<SyncFile> instance = new AtomicReference<SyncFile>();
 22     
 23     private String currentDate = SyncFile.getCurrentDate();
 24     
 25     private File file = null;
 26     
 27     private FileWriter fw = null;
 28     
 29     private BufferedWriter bw = null;
 30     
 31     private SyncFile(String dir, String name) {
 32         SyncFile.FILEDIR = dir;
 33         SyncFile.FILENAME = name ;
 34         while(dir.endsWith("/")) {
 35             dir = dir.substring(0, dir.length() - 1);
 36         }
 37         mkDir(dir);
 38         String filePath = dir + "/" + name + "." + currentDate ;
 39         file = new File(filePath);
 40         try {
 41             if(!file . exists()) {
 42                 file.createNewFile();
 43             }
 44             fw = new FileWriter(file, true);
 45             bw = new BufferedWriter(fw);
 46         } catch (Exception e) {
 47             e.printStackTrace();
 48         }
 49         
 50     }
 51     
 52     public static SyncFile getInstance(String dir, String name) {
 53         if( instance .get() == null ) {
 54             instance .compareAndSet(null, new SyncFile(dir, name));
 55         }
 56         return instance.get();
 57     }
 58     
 59     public static SyncFile getInstance() {
 60         if( instance .get() == null ) {
 61             instance.compareAndSet(null, new SyncFile(FILEDIR, FILENAME));
 62         }
 63         return instance.get();
 64     }
 65     
 66     public synchronized void write (String line) {
 67         openBuffer() ;
 68         
 69         line = line.endsWith("\n") ? line : line + "\n"; 
 70         try {
 71             if(line != null && line.trim() != ""){
 72                 bw.write(line);
 73                 bw.flush();
 74             }
 75         } catch (IOException e) {
 76             e.printStackTrace();
 77         } 
 78     }
 79     
 80     private static void mkDir( String dir) {
 81         File file = new File(dir);
 82         mkDir(file);
 83     }
 84     
 85     /**
 86      * <p>递归创建不存在的目录
 87      * @param file
 88      */
 89     private static void mkDir(File file){
 90         if(file.getParentFile().exists()){
 91             file.mkdir();
 92         }else{
 93             mkDir(file.getParentFile());
 94             file.mkdir();
 95         }
 96     }
 97     
 98     public static String getCurrentDate() {
 99         return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
100     }
101     
102     private void openBuffer() {
103         
104         if(!currentDate.equals(SyncFile.getCurrentDate()) || fw == null ||bw == null) {
105             if (!currentDate.equals(SyncFile.getCurrentDate())){
106                 currentDate = SyncFile.getCurrentDate();
107                 file = new File(FILEDIR + "/" + FILENAME + "." + currentDate);
108             }
109             close ();
110             try {
111                 fw = new FileWriter(file , true);
112                 bw = new BufferedWriter(fw);
113             } catch (IOException e) {
114                 e.printStackTrace();
115             }
116         }
117         
118     }
119     
120     public void close() {
121         try {
122             if (bw != null) {
123                 bw.close();
124                 bw = null;
125             }
126                 
127             if (fw != null) {
128                 fw.close();
129                 fw = null;
130             }
131         } catch (IOException e) {
132             e.printStackTrace();
133         }
134     }
135     
136     public static void main(String[] args) {
137 //        String a = "/data/logs/////";
138 //        while (a.endsWith("/")) {
139 //            a = a.substring(0 , a.length()-1);
140 //            System.out.println(a);
141 //        }
142 //        System.out.println("ok" + a);
143         mkDir("c:/2");
144     }
145     
146     
147 }
mamicode.com,码迷

 

通用的日志记录器(java),码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!