Class RollingFileAppender

java.lang.Object
com.log4rich.appenders.RollingFileAppender
All Implemented Interfaces:
Appender

public class RollingFileAppender extends Object implements Appender
File appender that rolls over based on file size. Supports compression of rolled files using external programs. This appender is thread-safe and handles automatic file rolling when the current log file reaches the configured maximum size.
  • Constructor Details

    • RollingFileAppender

      public RollingFileAppender()
      Creates a new RollingFileAppender with default settings. Uses "logs/application.log" as the default file path.
    • RollingFileAppender

      public RollingFileAppender(String filePath)
      Creates a new RollingFileAppender with the specified file path.
      Parameters:
      filePath - the path to the log file
    • RollingFileAppender

      public RollingFileAppender(File file)
      Creates a new RollingFileAppender with the specified file. Initializes with default settings: 10MB max size, 10 backup files, compression enabled, UTF-8 encoding, and immediate flush.
      Parameters:
      file - the log file to write to
  • Method Details

    • append

      public void append(LoggingEvent event)
      Appends a log event to the file, handling rollover if necessary. This method is thread-safe and handles file initialization, rollover checks, and error recovery.
      Specified by:
      append in interface Appender
      Parameters:
      event - the logging event to append
    • close

      public void close()
      Closes this appender and releases all resources. Flushes any remaining data and closes the file writer.
      Specified by:
      close in interface Appender
    • setFile

      public void setFile(String filePath)
      Sets the file path for this appender.
      Parameters:
      filePath - the path to the log file
    • setFile

      public void setFile(File file)
      Sets the file for this appender. If a writer is currently open, it will be closed and a new one created.
      Parameters:
      file - the log file to write to
    • setMaxFileSize

      public void setMaxFileSize(long maxFileSize)
      Sets the maximum file size in bytes before rollover occurs.
      Parameters:
      maxFileSize - the maximum file size in bytes
    • setMaxFileSize

      public void setMaxFileSize(String maxFileSize)
      Sets the maximum file size using a string format (e.g., "10M", "100K", "1G").
      Parameters:
      maxFileSize - the maximum file size as a string
    • setMaxBackups

      public void setMaxBackups(int maxBackups)
      Sets the maximum number of backup files to keep.
      Parameters:
      maxBackups - the maximum number of backup files
    • setCompression

      public void setCompression(boolean compression)
      Sets whether backup files should be compressed.
      Parameters:
      compression - true to enable compression, false to disable
    • setCompressionManager

      public void setCompressionManager(CompressionManager compressionManager)
      Sets the compression manager to use for compressing backup files.
      Parameters:
      compressionManager - the compression manager to use
    • setEncoding

      public void setEncoding(Charset encoding)
      Sets the character encoding for the log file.
      Parameters:
      encoding - the character encoding to use
    • setImmediateFlush

      public void setImmediateFlush(boolean immediateFlush)
      Sets whether to flush the output stream immediately after each write.
      Parameters:
      immediateFlush - true to enable immediate flushing, false otherwise
    • setBufferSize

      public void setBufferSize(int bufferSize)
      Sets the buffer size for the file writer.
      Parameters:
      bufferSize - the buffer size in bytes
    • setDatePattern

      public void setDatePattern(String datePattern)
      Sets the date pattern used for backup file naming.
      Parameters:
      datePattern - the date pattern string (e.g., "yyyy-MM-dd-HH-mm-ss")
    • setLayout

      public void setLayout(Layout layout)
      Sets the layout for formatting log events.
      Specified by:
      setLayout in interface Appender
      Parameters:
      layout - the layout to use, or null for default StandardLayout
    • getLayout

      public Layout getLayout()
      Gets the current layout used for formatting log events.
      Specified by:
      getLayout in interface Appender
      Returns:
      the current layout
    • setLevel

      public void setLevel(LogLevel level)
      Sets the minimum log level for this appender.
      Specified by:
      setLevel in interface Appender
      Parameters:
      level - the minimum log level, or null to use TRACE
    • getLevel

      public LogLevel getLevel()
      Gets the minimum log level for this appender.
      Specified by:
      getLevel in interface Appender
      Returns:
      the minimum log level
    • isLevelEnabled

      public boolean isLevelEnabled(LogLevel level)
      Checks if the specified log level is enabled for this appender.
      Specified by:
      isLevelEnabled in interface Appender
      Parameters:
      level - the log level to check
      Returns:
      true if the level is enabled, false otherwise
    • isClosed

      public boolean isClosed()
      Checks if this appender is closed.
      Specified by:
      isClosed in interface Appender
      Returns:
      true if the appender is closed, false otherwise
    • setName

      public void setName(String name)
      Sets the name of this appender.
      Specified by:
      setName in interface Appender
      Parameters:
      name - the name to set, or null for default "RollingFile"
    • getName

      public String getName()
      Gets the name of this appender.
      Specified by:
      getName in interface Appender
      Returns:
      the appender name
    • getFile

      public File getFile()
      Gets the current log file.
      Returns:
      the log file
    • getMaxFileSize

      public long getMaxFileSize()
      Gets the maximum file size before rollover.
      Returns:
      the maximum file size in bytes
    • getMaxBackups

      public int getMaxBackups()
      Gets the maximum number of backup files to keep.
      Returns:
      the maximum number of backup files
    • isCompression

      public boolean isCompression()
      Checks if compression is enabled for backup files.
      Returns:
      true if compression is enabled, false otherwise
    • getCompressionManager

      public CompressionManager getCompressionManager()
      Gets the compression manager used for backup files.
      Returns:
      the compression manager