Log4j2 Configuration 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 the log4j2 configuration. Here are a few examples of the Log4j2 configuration for your reference.

1. Log4j2 Properties File examples


1.1. Log4j2 example for properties to 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.

1.2. Log4j2 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

2. Log4j2 example for yaml file


2.1. Log4j2 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

2.2. 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

3. Log4j2 example for json file


3.1. Log4j2 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"
                }
            }
        }
    }
}

3.2. Log4j2 properties 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"
                }
            }
        }
    }
}

4. Log4j2 example for xml file


4.1. Log4j2 configuration 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= ”[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} – %msg%n” />
        </Console>
    </Appenders>
    <Loggers>
        <Root level= ”debug” additivity= ”false”>
            <AppenderRef ref= ”console” />
        </Root>
    </Loggers>
</Configuration>

4.2. Log4j2 configuration 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”></timebasedtriggeringpolicy>
            </policies>
        </rollingfile>

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