log4j - FileAppender



A FileAppender is an object of ObjectStreamAppender and is used to write to a file named using fileName parameter in log4j configuration.

log4j2.properties

appender.0.type = File
appender.0.name = logFile

log4j2.xml

<Appenders>
   <File name="logFile" fileName="logs/app.log">
      <PatternLayout>
         <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
   </File>
</Appenders>

FileAppender uses FileManger, a subclass of OutputStreamManager to perform the I/O.

FileAppender Configuration

FileAppender has the following configurable parameters:

Property Type Default Value Description
fileName Path   The path to the current log file If the folder containing the file does not exist, it will be created.
name String   The name of the appender.
bufferSize int 8192 The size of the ByteBuffer internally used by the appender.
ignoreExceptions boolean true If false, logging exception will be forwarded to the caller of the logging statement. Otherwise, they will be ignored.
immediateFlush boolean true If set to true, the appender will flush its internal buffer after each event.
filter String   Allows filtering log events just before they are formatted and sent.
layout Layout   Formats log events.
append boolean true If true, the log file will be opened in APPEND mode. On most systems this guarantees atomic writes to the end of the file, even if the file is opened by multiple applications.
bufferedIo boolean true If set to true, Log4j Core will format each log event in an internal buffer, before sending it to the underlying resource.
createOnDemand boolean false The appender creates the file on-demand. The appender only creates the file when a log event passes all filters and is routed to this appender. Defaults to false.
filePermissions PosixFilePermissions null If not null, it specifies the POSIX file permissions to apply to each created file. The permissions must be provided in the format used by PosixFilePermissions.fromString(), e.g. rw-rw----. The underlying files system shall support POSIX file attribute view.
fileOwner String null If not null, it specifies the file owner to apply to each created file. The underlying files system shall support file owner attribute view.
fileGroup String null If not null, it specifies the file group owner to apply to each created file. The underlying files system shall support POSIX file attribute view.
locking boolean false If true, Log4j will lock the log file at each log event. Note that the effects of this setting depend on the Operating System: some systems like most POSIX OSes do not offer mandatory locking, but only advisory file locking. This setting can also reduce the performance of the appender.

RollFile Appender Configuration Examples

Following is a sample configuration file log4j2.properties for FileAppender −

log4j2.properties

# Define the file appender
appender.0.type = File

# Set the name of the file appender
appender.0.name = logFile

# Set the name of the file
appender.0.fileName = logs/app.log

# Define the layout for file appender
appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %d %p %c{1.} [%t] %m%n

# Define the root logger with appender file
rootLogger.level = DEBUG
rootLogger.appenderRef.0.ref = logFile

If you wish to have an XML configuration file equivalent to the above log4j.properties file, then here is the content:

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="logFile" packages="">
   <Appenders>
      <File name="logFile" fileName="logs/app.log">
      <PatternLayout>
         <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      </File>
   </Appenders>
   <Loggers>
      <Root level="debug">
         <AppenderRef ref="logFile"/>
      </Root>
   </Loggers>
</Configuration>

Example - Usage of FileAppender

Now consider the following Java Example which would generate logging information:

Log4jDemo.java

package com.tutorialspoint;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jDemo{

   /* Get actual class name to be printed on */
   private static final Logger LOGGER = LogManager.getLogger();

   public static void main(String[] args) {
      LOGGER.debug("Hello this is an debug message");
      LOGGER.info("Hello this is an info message");
   }
}

We're using log4j2.properties file.

Output

Compile and run the above program. It would create a app.log file in logs directory which would have the following log information:

2025-09-06 07:05:27,262 DEBUG c.t.Log4jDemo [main] Hello this is an debug message
2025-09-06 07:05:27,264 INFO c.t.Log4jDemo [main] Hello this is an info message
Advertisements