FileDocCategorySizeDatePackage
FileHandler.javaAPI DocApache Tomcat 6.0.147644Fri Jul 20 04:20:36 BST 2007org.apache.juli

FileHandler

public class FileHandler extends Handler
Implementation of Handler that appends log messages to a file named {prefix}.{date}.{suffix} in a configured directory, with an optional preceding timestamp.
version
$Revision: 483782 $ $Date: 2006-12-08 03:24:30 +0100 (ven., 08 déc. 2006) $

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.
private String
prefix
The prefix that is added to log file filenames.
private String
suffix
The suffix that is added to log file filenames.
private PrintWriter
writer
The PrintWriter to which we are currently logging, if any.
Constructors Summary
public FileHandler()

        this(null, null, null);
    
public FileHandler(String directory, String prefix, String suffix)

        this.directory = directory;
        this.prefix = prefix;
        this.suffix = suffix;
        configure();
        open();
    
Methods Summary
public voidclose()
Close the currently open log file (if any).

        
        try {
            if (writer == null)
                return;
            writer.write(getFormatter().getTail(this));
            writer.flush();
            writer.close();
            writer = null;
            date = "";
        } catch (Exception e) {
            reportError(null, e, ErrorManager.CLOSE_FAILURE);
        }
        
    
private voidconfigure()
Configure from LogManager properties.


        Timestamp ts = new Timestamp(System.currentTimeMillis());
        String tsString = ts.toString().substring(0, 19);
        date = tsString.substring(0, 10);

        String className = FileHandler.class.getName();
        
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        
        // Retrieve configuration of logging file name
        if (directory == null)
            directory = getProperty(className + ".directory", "logs");
        if (prefix == null)
            prefix = getProperty(className + ".prefix", "juli.");
        if (suffix == null)
            suffix = getProperty(className + ".suffix", ".log");

        // Get logging level for the handler
        setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));

        // Get filter configuration
        String filterName = getProperty(className + ".filter", null);
        if (filterName != null) {
            try {
                setFilter((Filter) cl.loadClass(filterName).newInstance());
            } catch (Exception e) {
                // Ignore
            }
        }

        // Set formatter
        String formatterName = getProperty(className + ".formatter", null);
        if (formatterName != null) {
            try {
                setFormatter((Formatter) cl.loadClass(formatterName).newInstance());
            } catch (Exception e) {
                // Ignore
            }
        } else {
            setFormatter(new SimpleFormatter());
        }
        
        // Set error manager
        setErrorManager(new ErrorManager());
        
    
public voidflush()
Flush the writer.


        try {
            writer.flush();
        } catch (Exception e) {
            reportError(null, e, ErrorManager.FLUSH_FAILURE);
        }
        
    
private java.lang.StringgetProperty(java.lang.String name, java.lang.String defaultValue)

        String value = LogManager.getLogManager().getProperty(name);
        if (value == null) {
            value = defaultValue;
        } else {
            value = value.trim();
        }
        return value;
    
private voidopen()
Open the new log file for the date specified by date.


        // Create the directory if necessary
        File dir = new File(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);
            writer.write(getFormatter().getHead(this));
        } catch (Exception e) {
            reportError(null, e, ErrorManager.OPEN_FAILURE);
            writer = null;
        }

    
public voidpublish(java.util.logging.LogRecord record)
Format and publish a LogRecord.

param
record description of the log event



    // --------------------------------------------------------- Public Methods


                       
        

        if (!isLoggable(record)) {
            return;
        }

        // 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();
                }
            }
        }

        String result = null;
        try {
            result = getFormatter().format(record);
        } catch (Exception e) {
            reportError(null, e, ErrorManager.FORMAT_FAILURE);
            return;
        }
        
        try {
            writer.write(result);
            writer.flush();
        } catch (Exception e) {
            reportError(null, e, ErrorManager.WRITE_FAILURE);
            return;
        }