Error while getting meta information about the page.

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:

PropertyTypeDefault ValueDescription
fileNamePath The path to the current log file If the folder containing the file does not exist, it will be created.
nameString The name of the appender.
bufferSizeint8192The size of the ByteBuffer internally used by the appender.
ignoreExceptionsbooleantrueIf false, logging exception will be forwarded to the caller of the logging statement. Otherwise, they will be ignored.
immediateFlushbooleantrueIf set to true, the appender will flush its internal buffer after each event.
filterString Allows filtering log events just before they are formatted and sent.
layoutLayout Formats log events.
appendbooleantrueIf 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.
bufferedIobooleantrueIf set to true, Log4j Core will format each log event in an internal buffer, before sending it to the underlying resource.
createOnDemandbooleanfalseThe 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.
filePermissionsPosixFilePermissionsnullIf 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.
fileOwnerStringnullIf not null, it specifies the file owner to apply to each created file. The underlying files system shall support file owner attribute view.
fileGroupStringnullIf not null, it specifies the file group owner to apply to each created file. The underlying files system shall support POSIX file attribute view.
lockingbooleanfalseIf 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