Log4j2 configuration file examples

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

Log4J2 Properties 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

</p>
<p>#The level of internal Log4j events that should be logged to the console.<br />
#Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal".<br />
status = error</p>
<p>#dest property value can be Either "err", which will send output to stderr, or a file path or URL.<br />
dest = err</p>
<p>#The name of the configuration.<br />
name = PropertiesConfigurationForConsole</p>
<p>filter.threshold.type = ThresholdFilter<br />
filter.threshold.level = debug</p>
<p>appender.console.type = Console<br />
appender.console.name = STDOUT<br />
appender.console.layout.type = PatternLayout<br />
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n</p>
<p>rootLogger.level = debug<br />
rootLogger.appenderRefs = stdout<br />
rootLogger.appenderRef.stdout.ref = STDOUT</p>
<p>

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

</p>
<p>status = error<br />
name = PropertiesConfig</p>
<p>#Make sure to change log file path as per your need<br />
property.filename = \tmp\debug.log</p>
<p>filters = threshold</p>
<p>filter.threshold.type = ThresholdFilter<br />
filter.threshold.level = debug</p>
<p>appenders = rolling</p>
<p>appender.rolling.type = RollingFile<br />
appender.rolling.name = RollingFile<br />
appender.rolling.fileName = ${filename}<br />
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz<br />
appender.rolling.layout.type = PatternLayout<br />
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n<br />
appender.rolling.policies.type = Policies<br />
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy<br />
appender.rolling.policies.time.interval = 1<br />
appender.rolling.policies.time.modulate = true<br />
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy<br />
appender.rolling.policies.size.size=10MB<br />
appender.rolling.strategy.type = DefaultRolloverStrategy<br />
appender.rolling.strategy.max = 20</p>
<p>loggers = rolling</p>
<p>#Make sure to change the package structure as per your application<br />
logger.rolling.name = com.kodehelp.sampleapp<br />
logger.rolling.level = debug<br />
logger.rolling.additivity = false<br />
logger.rolling.appenderRef.rolling.ref = RollingFile</p>
<p>

Log4j2 Configuration example for yaml file

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

</p>
<p>Configuration:<br />
status: error<br />
dest: err</p>
<p>Appenders:<br />
Console:<br />
name: Console<br />
target: SYSTEM_OUT<br />
PatternLayout:<br />
Pattern: %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n</p>
<p>Loggers:<br />
Root:<br />
level: debug<br />
AppenderRef:<br />
ref: Console</p>
<p>

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

</p>
<p>Configuration:<br />
status: error<br />
dest: err</p>
<p>Appenders:<br />
RollingFile:<br />
- name: RollingFile_Appender<br />
fileName: \tmp\rollingfile.log<br />
filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"<br />
PatternLayout:<br />
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"<br />
Policies:<br />
SizeBasedTriggeringPolicy:<br />
size: 1 KB<br />
DefaultRollOverStrategy:<br />
max: 30</p>
<p>Loggers:<br />
Logger:<br />
- name: com.kodehelp.sampleapp<br />
level: debug<br />
AppenderRef:<br />
- ref: File_Appender<br />
level: error<br />
- ref: RollingFile_Appender<br />
level: debug</p>
<p>

Log4j2 Configuration example for json file

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

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

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

<br />
{<br />
   "configuration": {<br />
      "name": "Default",<br />
      "appenders": {<br />
         "RollingFile": {<br />
            "name":"File",<br />
            "fileName":"\tmp\rollingFile.log",<br />
            "filePattern":"logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz",<br />
            "PatternLayout": {<br />
               "pattern":"%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"<br />
            },<br />
            "Policies": {<br />
               "SizeBasedTriggeringPolicy": {<br />
                  "size":"10 MB"<br />
               }<br />
            },<br />
            "DefaultRolloverStrategy": {<br />
               "max":"10"<br />
            }<br />
         }<br />
      },<br />
      "loggers": {<br />
         "root": {<br />
            "level":"debug",<br />
            "appender-ref": {<br />
              "ref":"File"<br />
            }<br />
         }<br />
      }<br />
   }<br />
}<br />

Log4j2 configuration example for xml file

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

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

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

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