Spring Boot log4j2 example 2018-05-09 06:02

It is easy to use log4j2 in spring boot. Create a maven project, structure like following.

│  ├─java
│  │  └─com
│  │      └─henryxi
│  │          └─log4j2
│  │                  AppConfig.java
│  │                  AppController.java
│  │
│  └─resources
│          log4j2.xml

Add the dependency in the pom file. In order to use log4j2 you have to exclusion spring-boot-starter-logging


The content of AppConfig and AppController:

public class AppConfig extends WebMvcConfigurerAdapter {
    private static Logger logger = LoggerFactory.getLogger(AppConfig.class);

    public static void main(String[] args) throws Exception {
        try {
            SpringApplication.run(AppConfig.class, args);
        } catch (Exception e) {
            logger.error("start error:", e);

public class AppController {
    private static Logger logger = LoggerFactory.getLogger(AppController.class);

    @RequestMapping(value = "/testLog4j2", method = RequestMethod.GET)
    public String testLog4j2() {
        String content = "hello spring boot log4j2.";
        logger.info("this is info log!");
        logger.error("this is error log!");
        logger.warn("this is warm log!");
        return content;

Put log4j2.xml in resources directory.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        <File name="FileAppender" fileName="/tmp/spring_boot.log" >
            <PatternLayout pattern="${LOG_PATTERN}"/>
        <Logger name="com" level="info" additivity="false">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="FileAppender" />

        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="FileAppender" />

Run the project and access localhost:8080/testLog4j2. The console will output the following log.

2018-05-09 17:56:59.880  INFO henry-PC --- [nio-8080-exec-1] c.h.l.AppController                      : this is info log!
2018-05-09 17:56:59.880 ERROR henry-PC --- [nio-8080-exec-1] c.h.l.AppController                      : this is error log!
2018-05-09 17:56:59.880  WARN henry-PC --- [nio-8080-exec-1] c.h.l.AppController                      : this is warm log!

The content of log is in /tmp/spring_boot.log.