基础9-Spring Boot自带日志

通过查看starter场景启动器的依赖

这里可以看到,Spring Boot其实早就替我们完成jar替换工作了,而我们也能在上一章节中通过POM文件也从侧面验证了;(在2.X版本中,jcl-over-slf4j包被移除了,看来,JCL用的是实在太少了)

当我们运行@SpringBootApplication注解的启动类的时候,通过IDEA的控制台, 其实能看到INFO级别的日志输出:

这里提一下log日志级别,级别从高到低依次是:error>warn>info>debug>trace;

如何修改默认设置?

首先打开Spring Boot 2.2.2.RELEASE版本的官网:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/appendix-application-properties.html#server-properties

这个自己去看吧

如何自定义日志设置?

如果,我不想使用Spring Boot默认设置,或者,默认设置无法达到我的要求,则可以自己写一个适合自己的配置;

打开官网https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/index.html

在打开的新页面中,打开custom log configuration

这里有详细的规程,例如:

如果文件命名是logback.xml,则配置文件就直接被logback加载;

如果文件命名是logback-spring.xml,则配置文件是由Spring Boot来加载、识别,logback反而无法识别该配置文件;这种命名方式优点可以在官网找到说明:

通俗来讲就是:当采用logback-spring.xml命名方式时,该配置文件是由Spring Boot来加载的,因此,在XML文件中,除了可以使用基本的logback标签,还可以使用Spring Boot所独有的<springProfile/>标签、<springProperty/>,而采用logback.xml命名方式时,则只能使用logback自己所独有的标签了,下面是Spring Boot 2.2.2.RELEASE官网案例:

如何切换到log4j2框架?

log4j2,可以说是最完美的日志框架,所以,这次就切换到这个最完美的日志框架中;

第1步:把场景启动器,从logging切换到log4j2;

第2步:快捷键ctrl + shift + alt + u打开依赖关系图,把logging所依赖、被依赖的关系,全部删掉;

第3步:快捷键ctrl + shift + alt + u打开依赖关系图,把log4j2所依赖、被依赖的关系,全部添加到<dependencies/>中;

PS:这里官网也有对应的POM代码,可以作为侧面验证

最终POM代码

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

<project xmlns="http://maven.apache.org/POM/4.0.0"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.springboot</groupId>

    <artifactId>springboot.test</artifactId>

    <version>1.0-SNAPSHOT</version>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.2.2.RELEASE</version>

        <relativePath/>

    </parent>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

            <exclusions>

                <exclusion>

                    <artifactId>spring-boot-starter-logging</artifactId>

                    <groupId>org.springframework.boot</groupId>

                </exclusion>

                <exclusion>

                    <groupId>org.slf4j</groupId>

                    <artifactId>slf4j-api</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>ch.qos.logback</groupId>

                    <artifactId>logback-classic</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>ch.qos.logback</groupId>

                    <artifactId>logback-core</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>org.slf4j</groupId>

                    <artifactId>log4j-over-slf4j</artifactId>

                </exclusion>

                <exclusion>

                    <groupId>org.slf4j</groupId>

                    <artifactId>jul-to-slf4j</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-configuration-processor</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

        </dependency>

        <dependency>

            <groupId>org.junit.jupiter</groupId>

            <artifactId>junit-jupiter</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-log4j2</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-slf4j-impl</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-core</artifactId>

        </dependency>

        <dependency>

            <groupId>org.apache.logging.log4j</groupId>

            <artifactId>log4j-jul</artifactId>

        </dependency>

        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>jul-to-slf4j</artifactId>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <version>2.2.2.RELEASE</version>

                <executions>

                    <execution>

                        <goals>

                            <goal>repackage</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

</project>

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页