spring boot整合log4j2日志

版本

spring boot:2.0.3.RELEASE

1、引入maven依赖

<!--log4j2 start 不使用spring boot自带的logback-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
		</exclusion>
		<exclusion>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-to-slf4j</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j2</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!--log4j2 end-->

2、在src\main\resources\下新建log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--如果程序运行过程中,配置信息发生变化,每30秒动态加载一次-->
<Configuration status="warn" monitorInterval="30">

    <!--自定义变量,后面可以使用${}引入变量-->
    <properties>
        <property name="path">./logs</property>
        <property name="charset">UTF-8</property>
        <!--%t 线程name-->
        <!--%F 生成日志所在的类名-->
        <!--%L 生成日志所在的行号-->
        <!--%m 具体日志信息-->
        <!--%n 换行符-->
        <property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout charset="${charset}" pattern="${pattern}" />
        </Console>

        <RollingFile name="InfoRollingFile" fileName="${path}/message.log" filePattern="${path}/$${date:yyyy-MM-dd}/message-%d{yyyy-MM-dd}-%i.log">
            <!-- 记录INFO以及更高级别的日志(WARN/ERROR)-->
            <!-- 日志级别从低到高:-->
            <!-- debug < info < warn < error < fatal -->
            <ThresholdFilter level="info"/>
            <PatternLayout charset="${charset}" pattern="${pattern}" />
            <Policies>
                <!--每次服务启动或重启时,生成一个新日志文件-->
                <OnStartupTriggeringPolicy />
                <!--第二天时,生成一个新的日志文件-->
                <TimeBasedTriggeringPolicy />
                <!--日志达到16M时,生成一个新日志文件-->
                <SizeBasedTriggeringPolicy size="16 MB" />
            </Policies>
        </RollingFile>

        <RollingFile name="ErrorRollingFile" fileName="${path}/error.log" filePattern="${path}/$${date:yyyy-MM-dd}/error-%d{yyyy-MM-dd}-%i.log">
            <!-- 记录ERROR日志-->
            <ThresholdFilter level="error"/>
            <PatternLayout charset="${charset}" pattern="${pattern}" />
            <Policies>
                <OnStartupTriggeringPolicy />
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="16 MB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="InfoRollingFile"/>
            <AppenderRef ref="ErrorRollingFile"/>
        </Root>
        <logger name="org.springframework" level="info"></logger>
        <logger name="org.apache" level="info"></logger>
    </Loggers>
</Configuration>

3、写一个controller测试一下

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestController {

    private static Logger logger = LogManager.getLogger(TestController.class);

    @RequestMapping(value = "/logs", method = RequestMethod.GET)
    public void fun(String name) {
        logger.debug("fun()方法入参:{}", name);
        logger.info("fun()方法入参:{}", name);
        logger.warn("fun()方法入参:{}", name);
        logger.error("fun()方法入参:{}", name);
    }
}

启动,地址栏输入http://localhost:8080/logs?name=123

4、生成的日志如图

spring boot整合log4j2日志_第1张图片

你可能感兴趣的:(spring)