一。
近期自己的项目想要一个记录日志的功能,而springboot本身就内置了日志功能,然而想要输入想要的日志,并且输出到磁盘,然后按天归档,或者日志的切分什么的,自带的日志仅仅具有简单的功能,百度了一番,总结如下,适合大多数的应用场景
二。
springboot的pom文件都会引一个parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent>
点进去这个parent,会有一个这个dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
再点进去就是2.0版本,所谓的它给你集成的各种包依赖,而且规定了版本号,其中有一个包如下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.0.RELEASE</version> </dependency>
再点进去
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> <version>2.0.0.RELEASE</version> <scope>compile</scope> </dependency>
再点,啊哈,出来了,这些都是原有的日志包,所以,不必再引依赖了,直接用就ok了,用法如下:
<dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.10.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jul-to-slf4j</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency>
============================分割线====================================
首先,官方推荐使用的xml名字的格式为:logback-spring.xml而不是logback.xml,至于为什么,因为带spring后缀的可以使用<springProfile>这个标签。
在resource下创建logback-spring.xml文件
内容如下,每行都有注释了
<?xml version="1.0" encoding="UTF-8"?>