Configuration File for Log4j 2.4.1

Q

How to write a simple configuration file for Log4j 2.4.1? I just want to use the basic functionalities to specify the log file name and the log level.

✍: FYIcenter.com

A

Below is a simple configuration file, log4j2.xml, that support the Log4j 2 API:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2015 FYIcenter.com -->
<Configuration status="WARN">
 <Appenders>
  <File name="MyAppender" fileName="HelloLog4j2.log" 
   bufferedIO="true">
   <PatternLayout 
    pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  </File>
  <Console name="Screen" target="SYSTEM_OUT">
   <PatternLayout 
    pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  </Console>
 </Appenders>
 <Loggers>
  <Logger name="HelloLog4j2" level="debug" additivity="false">
   <AppenderRef ref="MyAppender"/>
  </Logger>
  <Root level="error">
   <AppenderRef ref="Screen"/>
  </Root>
 </Loggers>
</Configuration>

This sample configuration file log4j2.xml does the following:

  • It defines a named logger "HelloLog4j2" and the Root logger.
  • It sets the "HelloLog4j2" logger level to "debug" and higher.
  • It sets the Root logger level to "error" and higher.
  • It defines a Console appender "Screen" and a File appender "MyAppender".
  • It sets both appenders with the same logging string pattern.

Back to FAQ for Apache Log4j 2.4.1.

2015-11-11, 1153👍, 0💬