Spring boot日志介绍
1.1 常用的日志框架分为接口库和实现库
1.2 spring 的日志介绍
- spring框架默认选择的是JCL
- spring boot框架默认选择的是SLF4j + Logback
1.3 SLF4J的使用
案例:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}
1.3.1与其他实现库整合图(官网):
1.3.2 如何解决多个框架默认日志不统一的问题?
- 第一步:排除其他日志框架
- 第二步:使用中间包替换原有日志包
- 第三步:导入slf4j实现包
1.4 spring boot + slf4j+Logback
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
1.4.1 在哪排除其他框架的默认日志包
排除spring 使用的commons-logging
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
1.4.2 使用中间包替换原有日志包
1.4.3 spring boot日志配置
- spring boot默认级别为info
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
private Logger logger = LoggerFactory.getLogger(ApplicationTests.class);
@Test
public void logTest() {
logger.trace("--This is trace log");
logger.debug("-- --This is debug log");
logger.info("-- -- --This is info log");
logger.warn("-- -- -- --This is warn log");
logger.error("-- -- -- -- --This is error log");
}
}
运行结果:
: -- -- --This is info log
: -- -- -- --This is warn log
: -- -- -- -- --This is error log
- 简单配置案例
#设置日志有颜色的输出
spring:
output:
ansi:
enabled: always
#日志配置
logging:
#日志级别设置
level:
#指定包生效
com.lvmama: debug
#日志文件路径(当前工程根目录下spring.log)
path: /log/springboot
- spring boot 其他日志配置
logging.file
logging.file.max-size
logging.file.max-history
logging.path
logging.pattern.console
logging.pattern.dateformat
logging.pattern.file
logging.pattern.level
PID
1.4.4 自定义日志配置
When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.
在resource目录下添加指定的log.xml,spring boot默认只用自定义的日志配置,如logback在resource目录下添加logback.xml或logback-spring.xml即可。不过官网推荐使用logback-spring的方式,因为可以使用<springProfile>高级特性,动态使用日志配置。
Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xmlor define a logging.config property.
测试:
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<springProfile name="dev">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ---dev--- [%thread] - %-5level %logger{50} - %msg%n</pattern>
</springProfile>
<springProfile name="!dev">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level %logger{50} - %msg%n</pattern>
</springProfile>
</layout>
</appender>
mvn spring-boot:run
控制台输出:
2018-05-20 17:55:44.708 - [main] - INFO com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 4288 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot)
2018-05-20 17:55:44.732 - [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE
2018-05-20 17:55:44.733 - [main] - INFO com.lvmama.SpringBootInstaller - No active profile set, falling back to default profiles: default
2018-05-20 17:55:45.224 - [background-preinit] - INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2018-05-20 17:55:48.158 - [main] - INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2018-05-20 17:55:48.236 - [main] - INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
mvn spring-boot:run -Drun.profiles=dev //指定dev环境
2018-05-20 17:59:25.500 ---dev--- [main] - INFO com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 2880 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot)
2018-05-20 17:59:25.505 ---dev--- [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE
2018-05-20 17:59:25.506 ---dev--- [main] - INFO com.lvmama.SpringBootInstaller - The following profiles are active: dev
2018-05-20 17:59:26.007 ---dev--- [background-preinit] - INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final
2018-05-20 17:59:27.749 ---dev--- [main] - INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2018-05-20 17:59:27.769 ---dev--- [main] - INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。