019-Spring Boot 日志

一、概述

spring-boot默认支持info级别的日志。

日志级别:trace、debug、info、warn、error、fatal、off【关闭】

二、配置日志级别

2.1、配置文件配置日志级别

在application.properties中添加

# logging.level.*  其中*表示包名 或者类名  root 表示所有
logging.level.root=DEBUG

  能够打印出spring-boot的日志,以及用户自定义日志

具体指定某个类或包,其他未指定使用默认info级别

logging.level.com.lhx.spring.springboot.service.UserService=DEBUG
logging.level.com.lhx.spring.springboot.dao.UserDao=DEBUG

2.2、启动参数增加启动参数

--debug

  能够打印出spring-boot的日志,不能打印用户自定义

2.3、在代码中

ConfigurableApplicationContext context = SpringApplication.run(App.class, "--debug=true");

  能够打印出spring-boot的日志,不能打印用户自定义

三、文件输出

1、配置指定目录输出

logging.file=e:/tmp/my.log

2、配置日志目录【此时日志名spring.log】

logging.path=e:/tmp/logs

以上两种,日志文件输出,文件大小10M,自动分割

四、输出格式

4.1、控制台以及文件pattern

logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n
logging.file.console=%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n

五、spring日志分析

5.1、默认原生

查看可知,spring-boot使用logback输出日志文件

在resources中添加logback.xml或logback-spring.xml(spring推荐)文件如下即可

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="consolelog" class="ch.qos.logback.core.ConsoleAppender">
        <layout>
            <pattern>%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level
                %logger{80} - %msg%n</pattern>
        </layout>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="consolelog" />
    </root>
</configuration>

5.2、更换log4j2

在使用spring-boot-starter之中

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

默认已经添加了日志引用,应先排除

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

添加log4j2.pom配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

增加log4j2.xml获log4j2-spring.xml(spring推荐)配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appenders>
        <Console name="console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" />

        </Console>
    </appenders>
    <loggers>
        <root level="DEBUG">
            <appender-ref ref="console" />
        </root>
    </loggers>
</configuration>

程序使用即可

5.3、相关源码

查看spring-boot-1.5.9.RELEASE.jar包中,可以查看支持以下四种

org.springframework.boot.logging
org.springframework.boot.logging.java
org.springframework.boot.logging.log4j2
org.springframework.boot.logging.logback

在org.springframework.boot.logging 中查看日志级别org.springframework.boot.logging.LogLevel

public enum LogLevel {

    TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

}

配置项在:org.springframework.boot.logging.LoggingSystemProperties类中

在org.springframework.boot.logging.logback,查看default.xml 、base.xml、file-appender.xml、console-appender.xml配置文件

原文地址:https://www.cnblogs.com/bjlhx/p/8711516.html