This article will cover all its options for your needed and show you how to configure log4j using file log4j.properties

1. Console Logging

The log4j.properties file given below will set up logging on console.

Line 1, we are initializing the root logger with log level INFO and giving it the name loggerId.

In line 2, we are setting an appender for rootLogger using its name loggerId. The appender is ConsoleAppender, which logs messages to console.

And line 3, we are setting the layout for the log statements.

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.ConsoleAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout

2. File Logging

The log4j.properties file given below will create a log file named example.log in the folder d:/logs. (the file name you can set a relative path, ex: ../logs/example.log)

If you are using log4j-1.2.11 or earlier, make sure that the folder d:/logs is already present as log4j will not create it. From version log4j-1.2.12 onwards, log4j itself will create the log folder if not already present.

The conversion pattern specified is %d [%t] %-5p (%F:%L) – %m%n

  • %d stands for current timestamp.
  • %t stands for java thread name.
  • %p stands for log level.
  • %F stands for name of class that logged the message.
  • %L stands for line number where the message is logged.
  • %m stands for the logged message string
  • %n stands for new line character
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.FileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=d:/logs/example.log

3. Custom Pattern

Setting tokens/terms to be logged for every message using log4j.properties. The pattern can be used to customize the format of the logged message. The supported formats are:

Format Specifier Description
%C The complete class name that has invoked the log
%d The timestamp when log request is invoked. Further customized using java’s DateFormat style of abbreviations like %d{yyyy-MM-dd HH:mm:ss,SSS} which incidentally is the default used.
%F The source file name that makes the log request.
%l The source file name, method name and line number where log request is made.
%L The line number of the source file where log request is made.
%m The actual message logged.
%M The method name that makes the log request.
%n New line character
%p The log level used in the log request.
%r The time elapsed between start of the application and the log request in milliseconds.
%t The java thread name that nakes the log request.

4. Rotate File On Size

How to make log files rotate based on size using log4j.properties

This log4j.properties file will create a log file named example.log in the folder d:/logs. If this file exceeds a size of 100KB, it will be renamed to example.log.1 and a replaced by a new example.log file. A maximum of 5 such backup files will be created.

If you are using log4j-1.2.11 or earlier, then you will need to create the folder d:/logs yourself, else log4j will fail to create the log file. In later versions, log4j creates it for you.

To configure max file size in mega-bytes, use MB as in 20MB.

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.RollingFileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=d:/logs/example.log
log4j.appender.loggerId.MaxFileSize=100KB
log4j.appender.loggerId.MaxBackupIndex=5

5. Rotate File Per Day

How to make log files rotate every day using log4j.properties
The log4j.properties file given below will create one log file per day, with name like example-20120801.log, example-20120802.log etc in d:/logs folder.The file will rollover at 12AM everyday.

To make the file rollover per month, per hour or per minute, only the DatePattern needs to be changed to yyyyMM/yyyyMMddHH/yyyyMMddHHmm respectively as shown in the next example

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.DailyRollingFileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=d:/logs/example
log4j.appender.loggerId.DatePattern='-'yyyyMMdd'.log'

6. Rotate File Per Hour/Minute

How to make log files rotate per minute or hour using log4j.properties

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.DailyRollingFileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=example
log4j.appender.loggerId.DatePattern='-'yyyyMMdd-HHmm'.log'

7. Rotate & Archiving File On Size

How to make log files rotate based on size and zip old rotated logs using log4j.properties

#Worked with 2.17 version
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.loggerId.rollingPolicy.maxIndex=5
log4j.appender.loggerId.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.loggerId.triggeringPolicy.MaxFileSize=10000000
log4j.appender.loggerId.rollingPolicy.FileNamePattern=worker-%i.log.gz
log4j.appender.loggerId.rollingPolicy.ActiveFileName=worker.log
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n

8. Rotate & Archiving File Per Day

How to make log files rotate per day and zip old rotated logs using log4j.properties

#Worked with 2.17 version
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.loggerId.rollingPolicy.ActiveFileName=worker.log
log4j.appender.loggerId.rollingPolicy.FileNamePattern=worker-.%d{yyyyMMdd}.log.gz
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n

9. Rotate & Archiving File Per Hour/Minute

How to make log files rotate every minute or hour and zip old rotated logs using log4j.properties

#Worked with 2.17 version
log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.loggerId.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.loggerId.rollingPolicy.ActiveFileName=worker.log
log4j.appender.loggerId.rollingPolicy.FileNamePattern=worker-.%d{yyyyMMdd-HHmm}.log.gz
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n

10. Multiple Appender

How to configure multiple appender for a logger using log4j.properties

log4j.rootLogger=INFO, loggerIdOne, loggerIdTwo 
log4j.appender.loggerIdOne=org.apache.log4j.ConsoleAppender
log4j.appender.loggerIdOne.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerIdTwo=org.apache.log4j.RollingFileAppender
log4j.appender.loggerIdTwo.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerIdTwo.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerIdTwo.File=example.log

11. Multiple Logger

How to configure multiple logger for the app using log4j.properties

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.ConsoleAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.logger.com.techfundaes.log4j.BasicUse=DEBUG, loggerId2
log4j.appender.loggerId2=org.apache.log4j.RollingFileAppender
log4j.appender.loggerId2.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId2.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId2.File=example.log

12. Append Config

Append Config : How to enable log appending across app restarts instead of overwrite using log4j.properties

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.RollingFileAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId.File=exampleee.log
log4j.appender.loggerId.append=false

13. Additivity Config

Additivity Config : How to stop a child logger’s log from getting written in its parent’s appender using log4j.properties

log4j.rootLogger=INFO, loggerId
log4j.appender.loggerId=org.apache.log4j.ConsoleAppender
log4j.appender.loggerId.layout=org.apache.log4j.PatternLayout
log4j.logger.com.techfundaes.log4j.BasicUse=DEBUG, loggerId2
log4j.appender.loggerId2=org.apache.log4j.RollingFileAppender
log4j.appender.loggerId2.layout=org.apache.log4j.PatternLayout
log4j.appender.loggerId2.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.loggerId2.File=example.log
log4j.additivity.com.techfundaes.log4j.BasicUse=false

About The Author