由于今日头条对于代码块的展示不是很友好,导致阅读不便,建议关注并私信作者回复【java】获取最新最具有观赏性的代码,谢谢给位支持!
ElasticSearch6.5.4单机部署以及简单尝试ElasticSearch6.5.4几个重要概念以及常用搜索ElasticSearch6.5.4中文以及拼音的操作ElasticSearch6.5.4java使用ES并实战搜索ElasticSearch6.5.4集群操作ElasticSearch6.5.4ELK和Logstash同步数据ElasticSearch6.5.4ES分布式原理以及工作原理ElasticSearch6.5.4ES常见问题
常用日志框架
目前最常用的是:SLF4J+Logback
Spring Boot日志框架使用
Spring Boot使用的日志框架是?
Spring Boot使用的日志框架正是SLF4J+Logback
Spring Boot如何使用日志框架?
1.在application.properties或者application.yaml文件中配置(可以忽略这步,直接到第二步,Spring Boot有自己的默认配置)
# LOGGINGlogging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logbacklogging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.logging.file= # Log file name. For instance `myapp.log`logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`logging.path= # Location of the log file. For instance `/var/log`logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
2.使用
如果什么都不配置,日志级别默认是info,并且只会在控制台输出,如果要想输出到文件,需要指定logging.path或者logging.file
package com.rancho.demo.spring.boot.demo.logging;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class LoggingApplicationTest { Logger logger = LoggerFactory.getLogger(getClass()); @Test public void loggingTest() { logger.trace("这是trace日志..."); logger.debug("这是debug日志..."); //默认输出级别 logger.info("这是info日志..."); logger.warn("这是warn日志..."); logger.error("这是error日志..."); }}
3.指定配置文件(可忽略)
如果需要指定自己的日志配置文件,只需要把你的logback.xml或者logback-spring.xml(logback-spring.xml和logback.xml的区别是:logback-spring.xml是由Spring Boot解析,可以用springProfile属性,而logback.xml是由logback解析)放在classpath下,这样Spring Boot就会加载自定义配置文件
上述就是Spring Boot的日志的基本使用
Spring Boot日志框架分析
1.为什么不需要导入对应的包就能直接使用
通过idea打开maven的show dependencies,会发现项目的整体依赖,
其中spring-boot-starter-web会依赖spring-boot-starter,
spring-boot-starter会依赖spring-boot-starter-logging,
spring-boot-starter-logging会依赖如下
可以看出Spring Boot自动导入了SLF4J+Logback,并且额外导入了jul-to-slf4j和log4j-to-slf4j
2.为什么不需要配置具体属性就可直接使用
上面说过有默认配置,默认配置的位置在spring-boot包下面
整合其他日志框架
比如导入其他框架的时候,其他框架使用的是log4j日志记录,我们想换成logback,上面说了spring-boot-starter-logging额外导入了jul-to-slf4j和log4j-to-slf4j,目的就是适配jul和log4j,只需在导入框架的时候排除log4j的实现就可以了
<>
切换到其他日志框架
slf4j切换日志框架参考:http://www.slf4j.org/legacy.html
切换至slf4j+log4j
org.springframework.boot spring-boot-starter-web logback-classic ch.qos.logback log4j-over-slf4j org.slf4jorg.slf4j slf4j-log4j12
Spring Boot Logging参考文档:
https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/boot-features-logging.html