A simple configuration to add log4j2 configuration xml
Dependency
Dependency should be added in the pom.xml file
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
Log4j2.xml
The file should be stored in the resources folder
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="DEBUG" />
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Log4j2HelloWorldExample.Java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4j2HelloWorldExample
{
private static final Logger LOGGER = LogManager.getLogger(Log4j2HelloWorldExample.class.getName());
public static void main(String[] args)
{
LOGGER.debug("Debug Message Logged !!!");
LOGGER.info("Info Message Logged !!!");
LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
}
}
Output:
12:48:52.590 [main] ERROR com.demo.spring.Log4j2HelloWorldExample - Error Message Logged !!!
java.lang.NullPointerException: NullError
at com.demo.spring.Log4j2HelloWorldExample.main(Log4j2HelloWorldExample.java:14) [classes/:?]
Awaiting for Administrator approval