26
26
import java.io.PrintWriter;
27
27
import java.io.UnsupportedEncodingException;
28
28
import java.sql.Timestamp;
29
import java.util.concurrent.locks.ReadWriteLock;
30
import java.util.concurrent.locks.ReentrantReadWriteLock;
29
31
import java.util.logging.ErrorManager;
30
32
import java.util.logging.Filter;
31
33
import java.util.logging.Formatter;
40
42
* named {prefix}.{date}.{suffix} in a configured directory, with an
41
43
* optional preceding timestamp.
43
* @version $Revision: 899160 $ $Date: 2010-01-14 12:13:46 +0100 (Do, 14. Jan 2010) $
45
* @version $Revision: 919742 $ $Date: 2010-03-06 12:15:07 +0100 (Sa, 06. Mär 2010) $
46
48
public class FileHandler
96
98
* The PrintWriter to which we are currently logging, if any.
98
100
private volatile PrintWriter writer = null;
103
private int bufferSize = 0;
104
* Lock used to control access to the writer.
106
protected ReadWriteLock writerLock = new ReentrantReadWriteLock();
112
private int bufferSize = -1;
106
115
// --------------------------------------------------------- Public Methods
122
131
String tsString = ts.toString().substring(0, 19);
123
132
String tsDate = tsString.substring(0, 10);
134
writerLock.readLock().lock();
125
135
// If the date has changed, switch log files
126
136
if (!date.equals(tsDate)) {
127
synchronized (this) {
137
// Update to writeLock before we switch
138
writerLock.readLock().unlock();
139
writerLock.writeLock().lock();
141
// Make sure another thread hasn't already done this
128
142
if (!date.equals(tsDate)) {
136
String result = null;
138
result = getFormatter().format(record);
139
} catch (Exception e) {
140
reportError(null, e, ErrorManager.FORMAT_FAILURE);
145
PrintWriter writer = this.writer;
147
writer.write(result);
148
if (bufferSize < 0) {
147
// Down grade to read-lock. This ensures the writer remains valid
148
// until the log message is written
149
writerLock.readLock().lock();
151
writerLock.writeLock().unlock();
156
String result = null;
158
result = getFormatter().format(record);
159
} catch (Exception e) {
160
reportError(null, e, ErrorManager.FORMAT_FAILURE);
166
writer.write(result);
167
if (bufferSize < 0) {
171
reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE);
152
reportError("FileHandler is closed or not yet initialized, unable to log ["+result+"]", null, ErrorManager.WRITE_FAILURE);
173
} catch (Exception e) {
174
reportError(null, e, ErrorManager.WRITE_FAILURE);
154
} catch (Exception e) {
155
reportError(null, e, ErrorManager.WRITE_FAILURE);
178
writerLock.readLock().unlock();
194
215
public void flush() {
217
writerLock.readLock().lock();
197
PrintWriter writer = this.writer;
201
222
} catch (Exception e) {
202
223
reportError(null, e, ErrorManager.FLUSH_FAILURE);
225
writerLock.readLock().unlock();
225
248
prefix = getProperty(className + ".prefix", "juli.");
226
249
if (suffix == null)
227
250
suffix = getProperty(className + ".suffix", ".log");
228
String sBufferSize = getProperty(className + ".bufferSize", "8192");
251
String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
230
253
bufferSize = Integer.parseInt(sBufferSize);
231
254
} catch (NumberFormatException ignore) {