Class AccessLogValve

  • All Implemented Interfaces:
    javax.management.MBeanRegistration, AccessLog, Contained, JmxEnabled, Lifecycle, Valve
    Direct Known Subclasses:
    ExtendedAccessLogValve

    public class AccessLogValve
    extends AbstractAccessLogValve
    This is a concrete implementation of AbstractAccessLogValve that outputs the access log to a file. The features of this implementation include:
    • Automatic date-based rollover of log files
    • Optional log file rotation

    For UNIX users, another field called checkExists is also available. If set to true, the log file's existence will be checked before each logging. This way an external log rotator can move the file somewhere and Tomcat will start with a new file.

    For JMX junkies, a public method called rotate has been made available to allow you to tell this instance to move the existing log file to somewhere else and start writing a new log file.

    • Field Detail

      • prefix

        protected volatile java.lang.String prefix
        The prefix that is added to log file filenames.
      • rotatable

        protected boolean rotatable
        Should we rotate our log file? Default is true (like old behavior)
      • renameOnRotate

        protected boolean renameOnRotate
        Should we defer inclusion of the date stamp in the file name until rotate time? Default is false.
      • suffix

        protected volatile java.lang.String suffix
        The suffix that is added to log file filenames.
      • writer

        protected java.io.PrintWriter writer
        The PrintWriter to which we are currently logging, if any.
      • fileDateFormatter

        protected java.text.SimpleDateFormat fileDateFormatter
        A date formatter to format a Date using the format given by fileDateFormat.
      • currentLogFile

        protected java.io.File currentLogFile
        The current log file we are writing to. Helpful when checkExists is true.
      • fileDateFormat

        protected java.lang.String fileDateFormat
        Date format to place in log file name.
      • encoding

        protected volatile java.lang.String encoding
        Character set used by the log file. If it is null, the system default character set will be used. An empty string will be treated as null when this property is assigned.
    • Constructor Detail

      • AccessLogValve

        public AccessLogValve()
    • Method Detail

      • getMaxDays

        public int getMaxDays()
      • setMaxDays

        public void setMaxDays​(int maxDays)
      • getDirectory

        public java.lang.String getDirectory()
        Returns:
        the directory in which we create log files.
      • setDirectory

        public void setDirectory​(java.lang.String directory)
        Set the directory in which we create log files.
        Parameters:
        directory - The new log file directory
      • isCheckExists

        public boolean isCheckExists()
        Check for file existence before logging.
        Returns:
        true if file existence is checked first
      • setCheckExists

        public void setCheckExists​(boolean checkExists)
        Set whether to check for log file existence before logging.
        Parameters:
        checkExists - true meaning to check for file existence.
      • getPrefix

        public java.lang.String getPrefix()
        Returns:
        the log file prefix.
      • setPrefix

        public void setPrefix​(java.lang.String prefix)
        Set the log file prefix.
        Parameters:
        prefix - The new log file prefix
      • isRotatable

        public boolean isRotatable()
        Should we rotate the access log.
        Returns:
        true if the access log should be rotated
      • setRotatable

        public void setRotatable​(boolean rotatable)
        Configure whether the access log should be rotated.
        Parameters:
        rotatable - true if the log should be rotated
      • isRenameOnRotate

        public boolean isRenameOnRotate()
        Should we defer inclusion of the date stamp in the file name until rotate time.
        Returns:
        true if the logs file names are time stamped only when they are rotated
      • setRenameOnRotate

        public void setRenameOnRotate​(boolean renameOnRotate)
        Set the value if we should defer inclusion of the date stamp in the file name until rotate time
        Parameters:
        renameOnRotate - true if defer inclusion of date stamp
      • isBuffered

        public boolean isBuffered()
        Is the logging buffered. Usually buffering can increase performance.
        Returns:
        true if the logging uses a buffer
      • setBuffered

        public void setBuffered​(boolean buffered)
        Set the value if the logging should be buffered
        Parameters:
        buffered - true if buffered.
      • getSuffix

        public java.lang.String getSuffix()
        Returns:
        the log file suffix.
      • setSuffix

        public void setSuffix​(java.lang.String suffix)
        Set the log file suffix.
        Parameters:
        suffix - The new log file suffix
      • getFileDateFormat

        public java.lang.String getFileDateFormat()
        Returns:
        the date format date based log rotation.
      • setFileDateFormat

        public void setFileDateFormat​(java.lang.String fileDateFormat)
        Set the date format date based log rotation.
        Parameters:
        fileDateFormat - The format for the file timestamp
      • getEncoding

        public java.lang.String getEncoding()
        Return the character set name that is used to write the log file.
        Returns:
        Character set name, or null if the system default character set is used.
      • setEncoding

        public void setEncoding​(java.lang.String encoding)
        Set the character set that is used to write the log file.
        Parameters:
        encoding - The name of the character set.
      • backgroundProcess

        public void backgroundProcess()
        Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container. Unexpected throwables will be caught and logged.
        Specified by:
        backgroundProcess in interface Valve
        Overrides:
        backgroundProcess in class ValveBase
      • rotate

        public void rotate()
        Rotate the log file if necessary.
      • rotate

        public boolean rotate​(java.lang.String newFileName)
        Rename the existing log file to something else. Then open the old log file name up once again. Intended to be called by a JMX agent.
        Parameters:
        newFileName - The file name to move the log file entry to
        Returns:
        true if a file was rotated with no error
      • log

        public void log​(java.io.CharArrayWriter message)
        Log the specified message to the log file, switching files if the date has changed since the previous log call.
        Specified by:
        log in class AbstractAccessLogValve
        Parameters:
        message - Message to be logged
      • open

        protected void open()
        Open the new log file for the date specified by dateStamp.