Package com.log4rich.appenders
Class RollingFileAppender
java.lang.Object
com.log4rich.appenders.RollingFileAppender
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionCreates a new RollingFileAppender with default settings.RollingFileAppender
(File file) Creates a new RollingFileAppender with the specified file.RollingFileAppender
(String filePath) Creates a new RollingFileAppender with the specified file path. -
Method Summary
Modifier and TypeMethodDescriptionvoid
append
(LoggingEvent event) Appends a log event to the file, handling rollover if necessary.void
close()
Closes this appender and releases all resources.Gets the compression manager used for backup files.getFile()
Gets the current log file.Gets the current layout used for formatting log events.getLevel()
Gets the minimum log level for this appender.int
Gets the maximum number of backup files to keep.long
Gets the maximum file size before rollover.getName()
Gets the name of this appender.boolean
isClosed()
Checks if this appender is closed.boolean
Checks if compression is enabled for backup files.boolean
isLevelEnabled
(LogLevel level) Checks if the specified log level is enabled for this appender.void
setBufferSize
(int bufferSize) Sets the buffer size for the file writer.void
setCompression
(boolean compression) Sets whether backup files should be compressed.void
setCompressionManager
(CompressionManager compressionManager) Sets the compression manager to use for compressing backup files.void
setDatePattern
(String datePattern) Sets the date pattern used for backup file naming.void
setEncoding
(Charset encoding) Sets the character encoding for the log file.void
Sets the file for this appender.void
Sets the file path for this appender.void
setImmediateFlush
(boolean immediateFlush) Sets whether to flush the output stream immediately after each write.void
Sets the layout for formatting log events.void
Sets the minimum log level for this appender.void
setMaxBackups
(int maxBackups) Sets the maximum number of backup files to keep.void
setMaxFileSize
(long maxFileSize) Sets the maximum file size in bytes before rollover occurs.void
setMaxFileSize
(String maxFileSize) Sets the maximum file size using a string format (e.g., "10M", "100K", "1G").void
Sets the name of this appender.
-
Constructor Details
-
RollingFileAppender
public RollingFileAppender()Creates a new RollingFileAppender with default settings. Uses "logs/application.log" as the default file path. -
RollingFileAppender
Creates a new RollingFileAppender with the specified file path.- Parameters:
filePath
- the path to the log file
-
RollingFileAppender
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
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. -
close
public void close()Closes this appender and releases all resources. Flushes any remaining data and closes the file writer. -
setFile
Sets the file path for this appender.- Parameters:
filePath
- the path to the log file
-
setFile
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
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
Sets the compression manager to use for compressing backup files.- Parameters:
compressionManager
- the compression manager to use
-
setEncoding
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
Sets the date pattern used for backup file naming.- Parameters:
datePattern
- the date pattern string (e.g., "yyyy-MM-dd-HH-mm-ss")
-
setLayout
Sets the layout for formatting log events. -
getLayout
Gets the current layout used for formatting log events. -
setLevel
Sets the minimum log level for this appender. -
getLevel
Gets the minimum log level for this appender. -
isLevelEnabled
Checks if the specified log level is enabled for this appender.- Specified by:
isLevelEnabled
in interfaceAppender
- 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. -
setName
Sets the name of this appender. -
getName
Gets the name of this appender. -
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
Gets the compression manager used for backup files.- Returns:
- the compression manager
-