Log4j2 configuration file examples

DeeKay January 6, 2017 at 4:14 am · How To

Log4J2 configuration through a configuration file 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 example for properties file

Log4j2 configuration example for properties file 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.policies.size.size=10MB
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


Configuration:
status: error
dest: err

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

Loggers:
Root:
level: debug
AppenderRef:
ref: Console

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


Configuration:
status: error
dest: err

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

Loggers:
Logger:
- name: com.kodehelp.sampleapp
level: debug
AppenderRef:
- 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": {
            "name":"File",
            "fileName":"\tmp\rollingFile.log",
            "filePattern":"logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz",
            "PatternLayout": {
               "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
            },
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size":"10 MB"
               }
            },
            "DefaultRolloverStrategy": {
               "max":"10"
            }
         }
      },
      "loggers": {
         "root": {
            "level":"debug",
            "appender-ref": {
              "ref":"File"
            }
         }
      }
   }
}

Log4j2 configuration example for xml file

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Properties>
        <Property name="basePath">\tmp\logs</Property>
    </Properties>
 
    <Appenders>
        <RollingFile name="fileLogger" fileName="${basePath}/rollingFile.log" filePattern="${basePath}/rolling-file-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
 
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout   pattern="&#91;%-5level&#93; %d{yyyy-MM-dd HH:mm:ss.SSS} &#91;%t&#93; %c{1} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.kodehelp.sampleapp" level="debug" additivity="true">
            <appender-ref ref="fileLogger" level="debug" />
        </Logger>
        <Root level="debug" additivity="false">
            <appender-ref ref="console" />
        </Root>
    </Loggers>
</Configuration>