Spring Boot log4j2 configuration example

2020-12-13 05:35

阅读:214

标签:default   app   name   public   ack   ogg   you   target   reporting   

 

spring boot 默认log用的是 LogBack。

1. Log4j2 Maven Dependency

pom.xml

org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASEUTF-8UTF-81.8org.springframework.boot
        spring-boot-starter-web
        org.springframework.boot
                spring-boot-starter-logging
            org.springframework.boot
        spring-boot-starter-log4j2
    

 2. Add log4j2.xml file in resources folder

如果存在log4j2 jar包,springboot会自动配置它。把 log4j2.xml放在 src/main/resources 文件夹。

 

%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSZ} %p %m%nc:/temp

 3. Spring boot log4j2 demo

Application.java

 

package com.howtodoinjava.app;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
 
    private static final Logger LOGGER = LogManager.getLogger(Application.class);
 
    public static void main(String[] args)
    {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
         
        LOGGER.info("Info level log message");
        LOGGER.debug("Debug level log message");
        LOGGER.error("Error level log message");
    }
}

 

 

Read More: Log4j2 properties file example

Spring Boot log4j2 configuration example

标签:default   app   name   public   ack   ogg   you   target   reporting   

原文地址:https://www.cnblogs.com/chenqr/p/11143572.html


评论


亲,登录后才可以留言!