KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > logger > LogFile


1 /**
2  * Copyright (C) 2005 manfred andres
3  * Created: 13.01.2005 (19:26:31)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package freecs.util.logger;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.nio.channels.WritableByteChannel JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.zip.GZIPOutputStream JavaDoc;
30
31 import freecs.Server;
32
33
34 /**
35  * LogFile is an object describing a logfile. LogFile will backup logfiles on a
36  * "per day" basis. OldLogfiles will have the same name with the
37  * suffix "_YYYYMMDD" (the current date).
38  * @author manfred andres
39  *
40  */

41 public class LogFile implements LogDestination {
42     private static SimpleDateFormat JavaDoc date = new SimpleDateFormat JavaDoc("yyyyMMdd");
43     
44     final String JavaDoc path;
45     FileOutputStream JavaDoc fos;
46     WritableByteChannel JavaDoc fc;
47     int logFileDay;
48     
49     
50     LogFile (String JavaDoc path) {
51         this.path = path;
52     }
53     
54     public WritableByteChannel JavaDoc getChannel() throws FileNotFoundException JavaDoc, IOException JavaDoc {
55         if (fc == null) {
56             fc = createNewLogFile();
57             return fc;
58         }
59         LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis());
60         if (logFileDay==LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH))
61             return fc;
62         backupLogFile();
63         fc = createNewLogFile();
64         return fc;
65     }
66     
67     private WritableByteChannel JavaDoc createNewLogFile () throws FileNotFoundException JavaDoc, IOException JavaDoc {
68         LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis());
69         File JavaDoc f = new File JavaDoc (path);
70         if (f.exists()) {
71             LogWriter.instance.cal.setTimeInMillis(f.lastModified());
72             if (logFileDay!=LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH))
73                backupLogFile();
74             f = new File JavaDoc (path);
75         }
76         logFileDay = LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH);
77         fos = new FileOutputStream JavaDoc (f);
78         return fos.getChannel();
79     }
80     
81     private void backupLogFile() throws IOException JavaDoc {
82         LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis());
83         String JavaDoc backupPath;
84         if (path.indexOf(".")> -1)
85             backupPath = path.substring(0, path.lastIndexOf("."))
86                     + "_" + date.format(LogWriter.instance.cal.getTime())
87                     + path.substring(path.lastIndexOf("."));
88         else
89             backupPath = path + "_" + date.format(LogWriter.instance.cal.getTime());
90         if (fos != null) {
91             fos.flush();
92             if (fc != null)
93                 fc.close();
94             fos.close();
95         }
96         File JavaDoc f = new File JavaDoc(path);
97         File JavaDoc backupFile = new File JavaDoc (backupPath);
98         f.renameTo(backupFile);
99         new LogFileShrinker(backupFile).start();
100     }
101     
102     public static void main (String JavaDoc arg[]) {
103         File JavaDoc f = new File JavaDoc (arg[0]);
104         LogFileShrinker lfs = new LogFileShrinker(f);
105         lfs.start();
106         System.out.println("Start");
107         try {
108             lfs.join();
109         } catch (Exception JavaDoc e) { }
110         System.out.println("DONE");
111     }
112     
113     
114     /**
115      * Subclass responsible for zipping backed up logfiles
116      */

117     static class LogFileShrinker extends Thread JavaDoc {
118         final File JavaDoc toZip;
119         
120         LogFileShrinker (File JavaDoc f) {
121             toZip = f;
122         }
123         
124         public void run() {
125             String JavaDoc originalFile = toZip.getAbsolutePath();
126             String JavaDoc compressedFile = originalFile + ".gz";
127             FileInputStream JavaDoc fis = null;
128             GZIPOutputStream JavaDoc gzos = null;
129             FileOutputStream JavaDoc fos = null;
130             try {
131                 try {
132                     fis = new FileInputStream JavaDoc (toZip);
133                 } catch (IOException JavaDoc ioe) {
134                     Server.debug ("LogFileShrinker", "Unable to open uncompressed file " + originalFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
135                     return;
136                 }
137                 
138                 try {
139                     File JavaDoc zipped = new File JavaDoc(toZip.getAbsolutePath() + ".gz");
140                     fos = new FileOutputStream JavaDoc (zipped);
141                 } catch (IOException JavaDoc ioe) {
142                     Server.debug ("LogFileShrinker", "Unable to open zip file-destination " + compressedFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
143                     return;
144                 }
145                 try {
146                     gzos = new GZIPOutputStream JavaDoc (fos);
147                     int read;
148                     while ((read = fis.read()) > -1)
149                         gzos.write(read);
150                     gzos.close();
151                     gzos=null;
152                     fos.close();
153                     fos=null;
154                     fis.close();
155                     fis=null;
156                     if (!toZip.delete())
157                         Server.log("LogFileShrinker", "Unable to delete original file " + originalFile, Server.MSG_ERROR, Server.LVL_MAJOR);
158                 } catch (IOException JavaDoc ioe) {
159                     Server.debug ("LogFileShrinker", "Unable to compress file " + originalFile + " to " + compressedFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
160                 }
161             } finally {
162                 if (fis != null) try {
163                     fis.close();
164                 } catch (Exception JavaDoc e) {
165                     // is there anything to do about these situations?
166
}
167                 if (gzos != null) try {
168                     gzos.close();
169                 } catch (Exception JavaDoc e) { }
170                 if (fos != null) try {
171                     fos.close();
172                 } catch (Exception JavaDoc e) { }
173             }
174         }
175     }
176 }
Popular Tags