Bootstrap

logback日志

前言

本篇博客主要介绍了如何在SpringBoot框架下打印输出日志,包括控制台的日志输出,将日志输出到文件,打印输出sql语句等功能。

一、添加依赖文件

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

但是呢,实际开发中是不需要直接添加该依赖的,你会发现spring-boot-starter其中包含了 spring-boot-starter-logging,该依赖内容就是 Spring Boot 默认的日志框架 logback。综上所述,只有你的依赖里面导入了spring-boot-starter相关依赖,就不需要导入上面的依赖了,比如说下面展示的web项目依赖。

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

二、文件配置

1.在application.yml中指定日志文件名

logging:
  config: classpath:logging-config.xml

2.在resources文件夹下面创建日志文件logging-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration  scan="true" scanPeriod="60 seconds" debug="false">
    <!--contextName标签可以用来设置logger文件名称,默认名称为“default,可以通过%contextName来打印名称-->
    <contextName>logback</contextName>
    <!--property标签的作用是用来定义变量,name是变量名
;