摘要:不过官网推荐使用的方式,因为可以使用高级特性动态使用日志配置。测试控制台输出指定环境
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</>复制代码
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
1.4.1 在哪排除其他框架的默认日志包
排除spring 使用的commons-logging
</>复制代码
org.springframework
spring-core
commons-logging
commons-logging
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的方式,因为可以使用
</>复制代码
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.
测试:
</>复制代码
%d{yyyy-MM-dd HH:mm:ss.SSS} ---dev--- [%thread] - %-5level %logger{50} - %msg%n
%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level %logger{50} - %msg%n
mvn spring-boot:run
控制台输出:
</>复制代码
2018-05-20 17:55:44.708 - [main] - INFO com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 4288 (D:workspacespring-boot
argetclasses started by Administrator in D:workspacespring-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:workspacespring-boot
argetclasses started by Administrator in D:workspacespring-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]
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/69505.html
摘要:作为微服务的基础设施之一,背靠强大的生态社区,支撑技术体系。微服务实践为系列讲座,专题直播节,时长高达小时,包括目前最流行技术,深入源码分析,授人以渔的方式,帮助初学者深入浅出地掌握,为高阶从业人员抛砖引玉。 简介 目前业界最流行的微服务架构正在或者已被各种规模的互联网公司广泛接受和认可,业已成为互联网开发人员必备技术。无论是互联网、云计算还是大数据,Java平台已成为全栈的生态体系,...
摘要:使用还是,根据个人喜好即可。如果错误页面也需要使用模板引擎动态生成,那么放在下面的路径。数据库自动配置嵌入式数据库如果类路径中包含或的相应包,那么就会自动配置这些嵌入式数据库的实例和数据源。 本文参考自Spring Boot文档。 Spring Boot 简介 Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了Spring Boot框架,它的作用很...
摘要:引入了新的环境和概要信息,是一种更揭秘与实战六消息队列篇掘金本文,讲解如何集成,实现消息队列。博客地址揭秘与实战二数据缓存篇掘金本文,讲解如何集成,实现缓存。 Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控 - 掘金Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...
阅读 2704·2021-10-19 11:41
阅读 2500·2021-09-01 10:32
阅读 3447·2019-08-29 15:21
阅读 1894·2019-08-29 12:20
阅读 1230·2019-08-29 12:13
阅读 704·2019-08-26 12:24
阅读 2609·2019-08-26 10:26
阅读 925·2019-08-23 18:40