FileDocCategorySizeDatePackage
FileLogger.javaAPI DocGlassfish v2 API9473Fri May 04 22:32:10 BST 2007org.apache.catalina.logger

FileLogger

public class FileLogger extends LoggerBase
Implementation of Logger that appends log messages to a file named {prefix}.{date}.{suffix} in a configured directory, with an optional preceding timestamp.
author
Craig R. McClanahan
version
$Revision: 1.4 $ $Date: 2007/05/05 05:32:09 $

Fields Summary
private String
date
The as-of date for the currently open log file, or a zero-length string if there is no open log file.
private String
directory
The directory in which log files are created.
protected static final String
info
The descriptive information about this implementation.
private String
prefix
The prefix that is added to log file filenames.
private org.apache.catalina.util.StringManager
sm
The string manager for this package.
private boolean
started
Has this component been started?
private String
suffix
The suffix that is added to log file filenames.
private boolean
timestamp
Should logged messages be date/time stamped?
private PrintWriter
writer
The PrintWriter to which we are currently logging, if any.
Constructors Summary
Methods Summary
private voidclose()
Close the currently open log file (if any)


        if (writer == null)
            return;
        writer.flush();
        writer.close();
        writer = null;
        date = "";

    
public java.lang.StringgetDirectory()
Return the directory in which we create log files.



    // ------------------------------------------------------------- Properties


                  
       

        return (directory);

    
public java.lang.StringgetPrefix()
Return the log file prefix.


        return (prefix);

    
public java.lang.StringgetSuffix()
Return the log file suffix.


        return (suffix);

    
public booleangetTimestamp()
Return the timestamp flag.


        return (timestamp);

    
public voidlog(java.lang.String msg)
Writes the specified message to a servlet log file, usually an event log. The name and type of the servlet log is specific to the servlet container.

param
msg A String specifying the message to be written to the log file


        // Construct the timestamp we will use, if requested
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        String tsString = ts.toString().substring(0, 19);
        String tsDate = tsString.substring(0, 10);

        // If the date has changed, switch log files
        if (!date.equals(tsDate)) {
            synchronized (this) {
                if (!date.equals(tsDate)) {
                    close();
                    date = tsDate;
                    open();
                }
            }
        }

        // Log this message, timestamped if necessary
        if (writer != null) {
            if (timestamp) {
                writer.println(tsString + " " + msg);
            } else {
                writer.println(msg);
            }
        }

    
private voidopen()
Open the new log file for the date specified by date.


        // Create the directory if necessary
        File dir = new File(directory);
        if (!dir.isAbsolute())
            dir = new File(System.getProperty("catalina.base"), directory);
        dir.mkdirs();

        // Open the current log file
        try {
            String pathname = dir.getAbsolutePath() + File.separator +
                prefix + date + suffix;
            writer = new PrintWriter(new FileWriter(pathname, true), true);
        } catch (IOException e) {
            writer = null;
        }

    
public voidsetDirectory(java.lang.String directory)
Set the directory in which we create log files.

param
directory The new log file directory


        String oldDirectory = this.directory;
        this.directory = directory;
        support.firePropertyChange("directory", oldDirectory, this.directory);

    
public voidsetPrefix(java.lang.String prefix)
Set the log file prefix.

param
prefix The new log file prefix


        String oldPrefix = this.prefix;
        this.prefix = prefix;
        support.firePropertyChange("prefix", oldPrefix, this.prefix);

    
public voidsetSuffix(java.lang.String suffix)
Set the log file suffix.

param
suffix The new log file suffix


        String oldSuffix = this.suffix;
        this.suffix = suffix;
        support.firePropertyChange("suffix", oldSuffix, this.suffix);

    
public voidsetTimestamp(boolean timestamp)
Set the timestamp flag.

param
timestamp The new timestamp flag


        boolean oldTimestamp = this.timestamp;
        this.timestamp = timestamp;
        support.firePropertyChange("timestamp", Boolean.valueOf(oldTimestamp),
                                   Boolean.valueOf(this.timestamp));

    
public voidstart()
Prepare for the beginning of active use of the public methods of this component. This method should be called after configure(), and before any of the public methods of the component are utilized.

exception
LifecycleException if this component detects a fatal error that prevents this component from being used


        // Validate and update our current component state
        if (started)
            throw new LifecycleException
                (sm.getString("fileLogger.alreadyStarted"));
        lifecycle.fireLifecycleEvent(START_EVENT, null);
        started = true;
        
        super.start();

    
public voidstop()
Gracefully terminate the active use of the public methods of this component. This method should be the last one called on a given instance of this component.

exception
LifecycleException if this component detects a fatal error that needs to be reported


        // Validate and update our current component state
        if (!started)
            throw new LifecycleException
                (sm.getString("fileLogger.notStarted"));
        lifecycle.fireLifecycleEvent(STOP_EVENT, null);
        started = false;

        close();
        
        super.stop();