Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
 import java.util.Map;
The FileRequestLog class is an implementation of the interface writing the log messages to an plain file. This class supports sharing the files for different log formatters, in that an internal map of log files is kept and access to the PrintWriters to write to the file is synchronized.

This class has a defined lifecycle to ensure correct operation: To ensure no log files are kept open, the RequestLoggerFilter object calls dispose() when it is being deactivated.

Note: Currently, each log file is kept open from the moment the log file is first moment until the dispose() method is called. Future development should probably focus on the following issues: (1) Implement an open/write/close cycle when logging a message, (2) close log files when the last user has closed the log, (3) optimize the first strategy by keeping the files open for some time.

Note: Currently, the PrintWriter used to log the message is flushed after each log message written. Future development should probably implement better buffering in conjunction with the temporary open/close cycles of the files.

 class FileRequestLog implements RequestLog {
     // The map of shared open files (actually PrintWriter instances)
     private static Map<StringPrintWriterlogFiles = new HashMap<StringPrintWriter>();
     // Dispose class by closing all open PrintWeiter instances
     static void dispose() {
         for (final Writer w : .values()) {
             try {
             } catch (IOException ioe) {
                 // don't care
     // The PrintWriter used by this instance to write the messages
     private PrintWriter output;
     FileRequestLog(File logFilethrows IOException {
         synchronized () {
             final String fileName = logFile.getAbsolutePath();
             this. = .get(fileName);
             if (this. == null) {
                 FileWriter fw = new FileWriter(logFiletrue);
                 this. = new PrintWriter(fw);

     public void write(String message) {
         // use a local copy of the reference to not encounter NPE when this
         // log happens to be closed asynchronously while at the same time not
         // requiring synchronization
         PrintWriter writer = this.;
         if (writer != null) {
             synchronized (writer) {
    public void close() {
        // just drop the reference to the output
        this. = null;
New to GrepCode? Check out our FAQ X