Javatpoint Logo
Javatpoint Logo

Log4j Properties

The log4j.properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

By default, the LogManager searches for a file named log4j.properties in the CLASSPATH.

log4j.properties Syntax:

  • The root logger's level is defined as DEBUG. The DEBUG will attach the appender named X to it.
  • Put the appender named X to be a valid appender.
  • Put the layout for the appender X.

Let's see the syntax of log4j.properties file for an appender X:

log4j.properties Example:

With the help of the above syntax, we define the following in log4j.properties file:

  • The root logger's level is defined as DEBUG, The DEBUG appender named FILE to it.
  • The appender FILE is defined as org.apache.log4j.FileAppender. It writes to a file with the name log.out located in the log directory.
  • The layout pattern specified is %m%n, which means the printed logging message will be followed by a newline character.

It is important to note that log4j supports UNIX-style variable substitution, such as ${variableName}.

log4j.properties - Other Examples:

  • Output to Console

In this example, all logging will be redirected to your console:

  • Output to a File

In this example, all Logging information will be redirected to your specified log file:

  • Output to Console and File

In this example, all logging will be redirected to both console and log file:


Next TopicLog4j XML





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA