简单实现:如何把所有的日志记录到同一个文件中???

java.util.logging.Logger

如何把所有的日志记录到同一个文件中???  自己做了个简单实现:

 

 1 package all;
 2 import java.io.IOException;
 3 import java.util.logging.FileHandler;
 4 import java.util.logging.Logger; // <= (1)
 5 import java.util.logging.SimpleFormatter;
 6 import java.util.logging.XMLFormatter;
 7 
 8 import fortestlog.Syspath;
 9 
10 public class Loggertest {
11     //------------------configure---------
12     public static Logger log =null; 
13     private static Loggertest loggertestinstance=null;
14     String filename="D:\\test\\1.txt";
15     int size=1024*1024;
16     int numb=10;
17     boolean bappend=true;//追加到一个文件中
18     FileHandler fh;
19     //------------------configure---------
20     
21     public Loggertest()
22     {
23         if(log==null){
24             //log=Logger.getLogger(Loggertest.class.getSimpleName());
25             //log=Logger.getLogger("");
26             log=Logger.getAnonymousLogger();
27 
28             try {
29                 fh = new FileHandler(filename,size,numb,bappend);
30                 
31                 //Simple
32                 //fh.setFormatter(new SimpleFormatter());
33                 //default
34                 //fh.setFormatter(new XMLFormatter());     
35                 log.addHandler(fh);
36             
37 
38             } catch (SecurityException e) {
39                 // TODO Auto-generated catch block
40                 e.printStackTrace();
41             } catch (IOException e) {
42                 // TODO Auto-generated catch block
43                 e.printStackTrace();
44             } 
45         }
46     }
47     
48     public static void dologtask(Object obj,String logcontent)
49     {
50         if(loggertestinstance==null){
51             
52             loggertestinstance=new Loggertest();
53         }
54         loggertestinstance.log.info(obj.getClass().toString()+":"+logcontent);
55     }
56     public static void dologtask(String logcontent)
57     {
58         if(loggertestinstance==null){
59             
60             loggertestinstance=new Loggertest();
61         }
62         loggertestinstance.log.info(logcontent);
63     }
64     public static void main(String[] args) {
65         
66          //Loggertest.dologtask(this,"from Syspath test!!!");
67          //call point
68          Loggertest.dologtask(new Syspath(),"!!! test  !!!");
69          Loggertest.dologtask("::: test  :::");
70     }
71 }

 

posted on 2014-08-26 16:21  rojas  阅读(361)  评论(0编辑  收藏  举报