1
2 package org.rpi.log;
3
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.Writer;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipOutputStream;
13
14 import org.apache.log4j.Layout;
15 import org.apache.log4j.RollingFileAppender;
16 import org.apache.log4j.helpers.CountingQuietWriter;
17 import org.apache.log4j.helpers.LogLog;
18
19
20
21 public class CustomRollingFileAppender extends RollingFileAppender {
22
23
24 private String mExtension;
25 private String mLogHeader;
26 private int mZipCounter;
27 private boolean mZipEnabled;
28
29 public CustomRollingFileAppender(Layout _pattern, String _file, String _extension, boolean _append) throws FileNotFoundException, IOException {
30 super(_pattern, _file + _extension, _append);
31 mExtension = _extension;
32 mLogHeader = null;
33 }
34
35
36 public void setZipEnabled(boolean _zip) {
37 mZipEnabled = _zip;
38 }
39
40 public boolean isZipEnabled() {
41 return mZipEnabled;
42 }
43
44 public void setHeaderParameter(String _header) {
45 mLogHeader = _header;
46 }
47
48 public void setMaximumFileSize(String _max) {
49 int length = _max.length();
50 String unit = "MB";
51 String value = _max;
52 if (length > 2) {
53 unit = _max.substring(length - 2);
54 value = _max.substring(0, length - 2);
55 }
56 if ("kB".equalsIgnoreCase(unit))
57 setMaximumFileSize(Integer.parseInt(value) * 1024);
58 else if ("MB".equalsIgnoreCase(unit))
59 setMaximumFileSize(Integer.parseInt(value) * 1024 * 1024);
60 else if ("GB".equalsIgnoreCase(unit))
61 {
62 long mValue = Integer.parseInt(value);
63 setMaximumFileSize(mValue * 1024 * 1024 * 1024);
64 }
65 else
66 setMaximumFileSize(Integer.parseInt(_max) * 1024 * 1024);
67 }
68
69 public void rollOver() {
70 String zipTargetName = "";
71 String targetName = "";
72 String fileRoot = fileName.substring(0, fileName.length() - mExtension.length());
73 LogLog.debug("Rolling over count=" + ((CountingQuietWriter) qw).getCount());
74 LogLog.debug("maxBackupIndex=" + maxBackupIndex);
75 if (maxBackupIndex > 0) {
76 File file = new File(fileRoot + '.' + maxBackupIndex + mExtension);
77 if (file.exists())
78 file.delete();
79 file = new File(fileRoot + '.' + maxBackupIndex + mExtension + ".zip");
80 if (file.exists())
81 file.delete();
82 File target;
83 if (mZipEnabled) {
84 for (int i = maxBackupIndex - 1; i >= 1; i--) {
85 file = new File(fileRoot + "." + i + mExtension + ".zip");
86 if (file.exists()) {
87 target = new File(fileRoot + '.' + (i + 1) + mExtension + ".zip");
88 LogLog.debug("Renaming file " + file + " to " + target);
89 file.renameTo(target);
90 }
91 }
92
93 } else {
94 for (int i = maxBackupIndex - 1; i >= 1; i--) {
95 file = new File(fileRoot + "." + i + mExtension);
96 if (file.exists()) {
97 target = new File(fileRoot + '.' + (i + 1) + mExtension);
98 LogLog.debug("Renaming file " + file + " to " + target);
99 file.renameTo(target);
100 }
101 }
102
103 }
104 zipTargetName = fileRoot + "-" + ++mZipCounter + mExtension;
105 targetName = fileRoot + ".1" + mExtension;
106 target = new File(mZipEnabled ? zipTargetName : targetName);
107 closeWriter();
108 file = new File(fileRoot + mExtension);
109 LogLog.debug("Renaming file " + file + " to " + target);
110 file.renameTo(target);
111 }
112 try {
113 setFile(fileRoot + mExtension, false, bufferedIO, bufferSize);
114 } catch (IOException e) {
115 LogLog.error("setFile(" + fileRoot + mExtension + ", false) call failed.", e);
116 }
117 if (mZipEnabled) {
118 final String zipName = targetName + ".zip";
119 final String fileToZip = zipTargetName;
120 (new Thread(new Runnable() {
121
122 public void run() {
123 try {
124 File file = new File(fileToZip);
125 FileInputStream fileIn = new FileInputStream(file);
126 long len = file.length();
127 byte tab[] = new byte[(int) len];
128 fileIn.read(tab);
129 fileIn.close();
130 ZipEntry zipEntry = new ZipEntry(fileToZip);
131 ZipEntry _tmp = zipEntry;
132 zipEntry.setMethod(8);
133 zipEntry.setSize(len);
134 FileOutputStream fileOut = new FileOutputStream(zipName);
135 ZipOutputStream zipOut = new ZipOutputStream(fileOut);
136 zipOut.putNextEntry(zipEntry);
137 zipOut.write(tab, 0, (int) len);
138 zipOut.flush();
139 zipOut.closeEntry();
140 zipOut.close();
141 file.delete();
142 } catch (FileNotFoundException fe) {
143 LogLog.warn("Error while trying to zip log file " + fileToZip + ": " + fe.getMessage());
144 } catch (IOException ioe) {
145 LogLog.warn("Error while trying to zip log file " + fileToZip + ": " + ioe.getMessage());
146 }
147 }
148
149 })).start();
150 }
151 }
152
153 public void activateOptions(boolean writeHeader) {
154 if (fileName != null) {
155 try {
156 setFile(fileName, fileAppend, bufferedIO, bufferSize, writeHeader);
157 } catch (IOException e) {
158 super.errorHandler.error("setFile(" + fileName + "," + fileAppend + ") call failed.", e, 4);
159 }
160 } else {
161 LogLog.warn("File option not set for appender [" + super.name + "].");
162 LogLog.warn("Are you using FileAppender instead of ConsoleAppender?");
163 }
164 }
165
166
167 public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize, boolean writeHeader) throws IOException {
168 LogLog.debug("setFile called: " + fileName + ", " + append);
169 if (bufferedIO)
170 setImmediateFlush(false);
171 reset();
172 FileOutputStream ostream = null;
173 try {
174 ostream = new FileOutputStream(fileName, append);
175 } catch (FileNotFoundException ex) {
176 String parentName = (new File(fileName)).getParent();
177 if (parentName != null) {
178 File parentDir = new File(parentName);
179 if (!parentDir.exists() && parentDir.mkdirs())
180 ostream = new FileOutputStream(fileName, append);
181 else
182 throw ex;
183 } else {
184 throw ex;
185 }
186 }
187 Writer fw = createWriter(ostream);
188 if (bufferedIO)
189 fw = new BufferedWriter(fw, bufferSize);
190 setQWForFiles(fw);
191 this.fileName = fileName;
192 fileAppend = append;
193 this.bufferedIO = bufferedIO;
194 this.bufferSize = bufferSize;
195 if (writeHeader) {
196 writeHeader();
197 }
198 if (append) {
199 File f = new File(fileName);
200 ((CountingQuietWriter) super.qw).setCount(f.length());
201 }
202 LogLog.debug("setFile ended");
203 }
204
205 }