Log4j2 configuration file example

Log4J2 Properties example of file configuration can be achieved in below 4 different file formats.

  • Properties file format – log4j2.properties
  • YAML file format – log4j2.yaml or log4j2.yml
  • JSON file format – log4j2.json
  • XML file format – log4j2.xml

I don’t see many examples available for log4j 2 configuration. Here are the few examples of Log4j 2 configuration for your reference.

Log4j2 Configuration file example

Log4j2 configuration for  redirect all logs to console

#The level of internal Log4j events that should be logged to the console.
#Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal".
status = error

#dest property value can be Either "err", which will send output to stderr, or a file path or URL.
dest = err

#The name of the configuration.
name = PropertiesConfigurationForConsole

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

NoteYou can use below log4j2.properties file logging output into console. Please note that if no configuration file could be located then DefaultConfiguration will be used. This also causes logging output to go to the console.

Log4j2 configuration example for properties file to redirect all logs to a file

status = error
name = PropertiesConfig

#Make sure to change log file path as per your need
property.filename = \tmp\debug.log

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = rolling

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20

loggers = rolling

#Make sure to change the package structure as per your application
logger.rolling.name = com.kodehelp.sampleapp
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

Log4j2 Configuration example for yaml file

Log4j2 Configuration example for yaml file to redirect all logs to console

status: error
dest: err

name: Console
target: SYSTEM_OUT
Pattern: %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

level: debug
ref: Console

Log4j2 configuration example for yaml file to redirect all logs to file

status: error
dest: err

- name: RollingFile_Appender
fileName: \tmp\rollingfile.log
filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
size: 1 KB
max: 30

- name: com.kodehelp.sampleapp
level: debug
- ref: File_Appender
level: error
- ref: RollingFile_Appender
level: debug

Log4j2 Configuration example for json file

Log4j2 configuration example for json file to redirect all logs to console

    "configuration": {
        "status": "error",
        "name": "jsonConsole",
        "packages": "com.kodehelp.sampleapp",
        "ThresholdFilter": {
            "level": "debug"
        "appenders": {
            "Console": {
                "name": "STDOUT",
                "PatternLayout": {
                    "pattern": %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
        "loggers": {
            "root": {
                "level": "debug",
                "AppenderRef": {
                    "ref": "STDOUT"

Log4j2 configuration example for json file to redirect all logs to file

   "configuration": {
      "name": "Default",
      "appenders": {
         "RollingFile": {
            "PatternLayout": {
               "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size":"10 MB"
            "DefaultRolloverStrategy": {
      "loggers": {
         "root": {
            "appender-ref": {

Log4j2 configuration example for xml file

Log4j2 configuration for xml file to redirect all logs to console

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n" />
        <Root level="debug" additivity="false">
            <AppenderRef ref="console" />

Log4j2 configuration for xml file to redirect all logs to file

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">
        <property name="basePath">\tmp\logs</property>

        <rollingfile name="fileLogger" fileName="${basePath}/rollingFile.log" filePattern="${basePath}/rolling-file-%d{yyyy-MM-dd}.log">
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n</pattern>
                <timebasedtriggeringpolicy interval="1" modulate="true"></timebasedtriggeringpolicy>

        <console name="console" target="SYSTEM_OUT">
            <patternlayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n"></patternlayout>
        <logger name="com.kodehelp.sampleapp" level="debug" additivity="true">
            <appender-ref ref="fileLogger" level="debug"></appender-ref>
        <root level="debug" additivity="false">
            <appender-ref ref="console"></appender-ref>

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.