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: Before using the class to instantiate it, the init(java.lang.String) method should be called mainly to set the root directory for relative log file paths. It is not recommended to call the init(java.lang.String) method multiple times. When the class is not used any more the dispose() method should be called to clean up, namely to close all open files. This lifecycle behaviour is forced by the RequestLogger component, which calls the init(java.lang.String) method on filter component activation and calls the dispose() metod on filter component deactivation.

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 file representing the root directory for relative log file paths
     private static File relPathRoot;
     // The map of shared open files (actually PrintWriter instances)
     private static Map<StringPrintWriterlogFiles = new HashMap<StringPrintWriter>();
     // Initialize class with the root directory for relative log file paths
     static void init(String relPathRoot) {
         . = new File(relPathRoot).getAbsoluteFile();
     // 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(String fileNamethrows IOException {
         // ensure the path is absolute
         File file = new File(fileName);
         if (!file.isAbsolute()) {
             file = new File(fileName);
         // get back the raw file name
         fileName = file.getAbsolutePath();
         synchronized () {
             this. = .get(fileName);
            if (this. == null) {
                // ensure location of the log file
                FileWriter fw = new FileWriter(filetrue);
                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