View | Details | Raw Unified | Return to bug 53620
Collapse All | Expand All

(-)a/java/org/apache/juli/AsyncFileHandler.java (-1 / +1 lines)
Lines 80-86 public class AsyncFileHandler extends FileHandler { Link Here
80
    }
80
    }
81
    
81
    
82
    @Override
82
    @Override
83
    protected void open() {
83
    public void open() {
84
        if(!closed) return;
84
        if(!closed) return;
85
        closed = false;
85
        closed = false;
86
        // TODO Auto-generated method stub
86
        // TODO Auto-generated method stub
(-)a/java/org/apache/juli/FileHandler.java (-66 / +64 lines)
Lines 96-156 public class FileHandler Link Here
96
        this.prefix = prefix;
96
        this.prefix = prefix;
97
        this.suffix = suffix;
97
        this.suffix = suffix;
98
        configure();
98
        configure();
99
        openWriter();
100
    }
99
    }
101
    
102
103
    // ----------------------------------------------------- Instance Variables
104
100
101
    public FileHandler(String directory, String prefix, String suffix, boolean rotatable, int bufferSize) {
102
        this.directory = directory;
103
        this.prefix = prefix;
104
        this.suffix = suffix;
105
        this.rotatable = rotatable;
106
        this.bufferSize = bufferSize;
107
        configure();
108
    }
105
109
106
    /**
110
    // ----------------------------------------------------- Instance Variables
107
     * The as-of date for the currently open log file, or a zero-length
108
     * string if there is no open log file.
109
     */
110
    private volatile String date = "";
111
111
112
112
113
    /**
113
    /**
114
     * The directory in which log files are created.
114
     * The directory in which log files are created.
115
     */
115
     */
116
    private String directory = null;
116
    private String directory;
117
118
117
119
    /**
118
    /**
120
     * The prefix that is added to log file filenames.
119
     * The prefix that is added to log file filenames.
121
     */
120
     */
122
    private String prefix = null;
121
    private String prefix;
123
124
122
125
    /**
123
    /**
126
     * The suffix that is added to log file filenames.
124
     * The suffix that is added to log file filenames.
127
     */
125
     */
128
    private String suffix = null;
126
    private String suffix;
129
130
127
131
    /**
128
    /**
132
     * Determines whether the logfile is rotatable
129
     * Determines whether the logfile is rotatable
133
     */
130
     */
134
    private boolean rotatable = true;
131
    private Boolean rotatable;
135
132
133
    /**
134
     * Log buffer size.
135
     */
136
    private Integer bufferSize;
136
137
137
    /**
138
    /**
139
     * The as-of date for the currently open log file, or null if there is no 
140
     * open log file.
141
     */
142
    private volatile String date = null;
143
    
144
    /**
138
     * The PrintWriter to which we are currently logging, if any.
145
     * The PrintWriter to which we are currently logging, if any.
139
     */
146
     */
140
    private volatile PrintWriter writer = null;
147
    private volatile PrintWriter writer = null;
141
148
142
143
    /**
149
    /**
144
     * Lock used to control access to the writer.
150
     * Lock used to control access to the writer.
145
     */
151
     */
146
    protected ReadWriteLock writerLock = new ReentrantReadWriteLock();
152
    protected final ReadWriteLock writerLock = new ReentrantReadWriteLock();
147
148
149
    /**
150
     * Log buffer size.
151
     */
152
    private int bufferSize = -1;
153
154
153
155
    // --------------------------------------------------------- Public Methods
154
    // --------------------------------------------------------- Public Methods
156
155
Lines 167-188 public class FileHandler Link Here
167
            return;
166
            return;
168
        }
167
        }
169
168
170
        // Construct the timestamp we will use, if requested
169
        final String tsDate = rotatable ? 
171
        Timestamp ts = new Timestamp(System.currentTimeMillis());
170
            new Timestamp(System.currentTimeMillis()).toString().substring(0, 10) : "";
172
        String tsString = ts.toString().substring(0, 19);
171
        
173
        String tsDate = tsString.substring(0, 10);
174
175
        try {
172
        try {
176
            writerLock.readLock().lock();
173
            writerLock.readLock().lock();
177
            // If the date has changed, switch log files
174
            // If the date has changed, switch log files
178
            if (rotatable && !date.equals(tsDate)) {
175
            // Construct the timestamp we will use, if requested
176
            // (... if not rotatable this will happen only once)
177
            if ( !tsDate.equals(date) ) {
179
                try {
178
                try {
180
                    // Update to writeLock before we switch
179
                    // Update to writeLock before we switch
181
                    writerLock.readLock().unlock();
180
                    writerLock.readLock().unlock();
182
                    writerLock.writeLock().lock();
181
                    writerLock.writeLock().lock();
183
    
182
184
                    // Make sure another thread hasn't already done this
183
                    // Make sure another thread hasn't already done this
185
                    if (!date.equals(tsDate)) {
184
                    if ( !tsDate.equals(date) ) {
186
                        closeWriter();
185
                        closeWriter();
187
                        date = tsDate;
186
                        date = tsDate;
188
                        openWriter();
187
                        openWriter();
Lines 206-212 public class FileHandler Link Here
206
            try {
205
            try {
207
                if (writer!=null) {
206
                if (writer!=null) {
208
                    writer.write(result);
207
                    writer.write(result);
209
                    if (bufferSize < 0) {
208
                    if (bufferSize == null || bufferSize < 0) {
210
                        writer.flush();
209
                        writer.flush();
211
                    }
210
                    }
212
                } else {
211
                } else {
Lines 243-249 public class FileHandler Link Here
243
            writer.flush();
242
            writer.flush();
244
            writer.close();
243
            writer.close();
245
            writer = null;
244
            writer = null;
246
            date = "";
245
            date = null;
247
        } catch (Exception e) {
246
        } catch (Exception e) {
248
            reportError(null, e, ErrorManager.CLOSE_FAILURE);
247
            reportError(null, e, ErrorManager.CLOSE_FAILURE);
249
        } finally {
248
        } finally {
Lines 257-263 public class FileHandler Link Here
257
     */
256
     */
258
    @Override
257
    @Override
259
    public void flush() {
258
    public void flush() {
260
259
        
261
        writerLock.readLock().lock();
260
        writerLock.readLock().lock();
262
        try {
261
        try {
263
            if (writer == null)
262
            if (writer == null)
Lines 271-303 public class FileHandler Link Here
271
        
270
        
272
    }
271
    }
273
    
272
    
274
    
275
    /**
273
    /**
276
     * Configure from <code>LogManager</code> properties.
274
     * Configure from <code>LogManager</code> properties.
277
     */
275
     */
278
    private void configure() {
276
    private void configure() {
279
277
        
280
        Timestamp ts = new Timestamp(System.currentTimeMillis());
278
        Timestamp ts = new Timestamp(System.currentTimeMillis());
281
        String tsString = ts.toString().substring(0, 19);
279
        date = ts.toString().substring(0, 10);
282
        date = tsString.substring(0, 10);
283
280
284
        String className = this.getClass().getName(); //allow classes to override
281
        String className = this.getClass().getName(); //allow classes to override
285
        
282
        
286
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
283
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
287
        
284
        
288
        // Retrieve configuration of logging file name
285
        // Retrieve configuration of logging file name
289
        rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
286
        if (rotatable == null)
287
            rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
290
        if (directory == null)
288
        if (directory == null)
291
            directory = getProperty(className + ".directory", "logs");
289
            directory = getProperty(className + ".directory", "logs");
292
        if (prefix == null)
290
        if (prefix == null)
293
            prefix = getProperty(className + ".prefix", "juli.");
291
            prefix = getProperty(className + ".prefix", "juli.");
294
        if (suffix == null)
292
        if (suffix == null)
295
            suffix = getProperty(className + ".suffix", ".log");
293
            suffix = getProperty(className + ".suffix", ".log");
296
        String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
294
        if (bufferSize == null) {
297
        try {
295
            String sBufferSize = getProperty(className + ".bufferSize", null);
298
            bufferSize = Integer.parseInt(sBufferSize);
296
            try {
299
        } catch (NumberFormatException ignore) {
297
                bufferSize = sBufferSize != null ? Integer.parseInt(sBufferSize) : null;
300
            //no op
298
            } catch (NumberFormatException ignore) { /* no op */ }
301
        }
299
        }
302
        // Get encoding for the logging file
300
        // Get encoding for the logging file
303
        String encoding = getProperty(className + ".encoding", null);
301
        String encoding = getProperty(className + ".encoding", null);
Lines 355-390 public class FileHandler Link Here
355
    /**
353
    /**
356
     * Open the new log file for the date specified by <code>date</code>.
354
     * Open the new log file for the date specified by <code>date</code>.
357
     */
355
     */
358
    protected void open() {
356
    public void open() {
359
        openWriter();
357
        openWriter();
360
    }
358
    }
361
    
359
    
362
    protected void openWriter() {
360
    protected void openWriter() {
363
361
        
364
        // Create the directory if necessary
362
        // Create the directory if necessary
365
        File dir = new File(directory);
363
        final File dir = new File(directory);
366
        if (!dir.mkdirs() && !dir.isDirectory()) {
364
        if ( !checkDir(dir) ) {
367
            reportError("Unable to create [" + dir + "]", null,
365
            writer = null; return;
368
                    ErrorManager.OPEN_FAILURE);
369
            writer = null;
370
            return;
371
        }
366
        }
372
367
        
373
        // Open the current log file
368
        // Open the current log file
374
        writerLock.writeLock().lock();
369
        writerLock.writeLock().lock();
370
        String logFileName = prefix + (rotatable ? date : "") + suffix;
375
        try {
371
        try {
376
            File pathname = new File(dir.getAbsoluteFile(), prefix
372
            File logFile = new File(dir.getAbsoluteFile(), logFileName);
377
                    + (rotatable ? date : "") + suffix);
373
            if ( !checkDir(logFile.getParentFile()) ) {
378
            File parent = pathname.getParentFile();
374
                writer = null; return;
379
            if (!parent.mkdirs() && !parent.isDirectory()) {
380
                reportError("Unable to create [" + parent + "]", null,
381
                        ErrorManager.OPEN_FAILURE);
382
                writer = null;
383
                return;
384
            }
375
            }
385
            String encoding = getEncoding();
376
            String encoding = getEncoding();
386
            FileOutputStream fos = new FileOutputStream(pathname, true);
377
            FileOutputStream fos = new FileOutputStream(logFile, true);
387
            OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
378
            OutputStream os = (bufferSize != null && bufferSize > 0) ? 
379
                    new BufferedOutputStream(fos, bufferSize) : fos;
388
            writer = new PrintWriter(
380
            writer = new PrintWriter(
389
                    (encoding != null) ? new OutputStreamWriter(os, encoding)
381
                    (encoding != null) ? new OutputStreamWriter(os, encoding)
390
                                       : new OutputStreamWriter(os), false);
382
                                       : new OutputStreamWriter(os), false);
Lines 398-402 public class FileHandler Link Here
398
390
399
    }
391
    }
400
392
393
    private boolean checkDir(final File dir) {
394
        if ( !dir.mkdirs() && !dir.isDirectory() ) {
395
            reportError("Unable to create [" + dir + "]", null, ErrorManager.OPEN_FAILURE);
396
            return false;
397
        }
398
        return true;
399
    }
401
400
402
}
401
}
403
- 

Return to bug 53620