SpringBoot深入简出之篇三
1. 自动配置原理
配置文件到底能写些什么属性呢?怎么写呢?自动配置原理;
配置文件能配置的属性参照
配置文件属性参照
#================================================= ==================
#COMMON SPRING BOOT PROPERTIES
##
此样本文件作为指南提供。不要将它的#complete复制
到您自己的应用程序中。^^^
#============================================== =====================
#----------------------------------------
#CORE PROPERTIES
#----- -----------------------------------
debug = false #启用调试日志。
trace = false #启用跟踪日志。
#logGING
logging.config = #日志配置文件的位置。例如,Logback的`classpath:logback.xml`。
logging.exception-conversion-word =%wEx #记录异常时使用的转换字。
logging.file = #日志文件名(例如,`myapp.log`)。名称可以是精确位置或相对于当前目录。
logging.file.max-history = 0 #要保留的归档日志文件的最大值。仅支持默认的logback设置。
logging.file.max-size = 10MB #最大日志文件大小。仅支持默认的logback设置。
logging.group。* =#记录组以同时快速更改多个记录器。例如,`logging.level.db = org.hibernate,org.springframework.jdbc`。
logging.level。* = #日志级别严重性映射。例如,`logging.level.org.springframework = DEBUG`。
logging.path = #日志文件的位置。例如,`/ var / log`。
logging.pattern.console = #用于输出到控制台的Appender模式。仅支持默认的Logback设置。
logging.pattern.dateformat = yyyy-MM-dd HH:mm:ss.SSS #日志日期格式的Appender模式。仅支持默认的Logback设置。
logging.pattern.file =#用于输出到文件的Appender模式。仅支持默认的Logback设置。
logging.pattern.level =%5p #日志级别的Appender模式。仅支持默认的Logback设置。
logging.register-shutdown-hook = false #在日志记录系统初始化时注册一个关闭钩子。
#AOP
spring.aop.auto =真#添加@EnableAspectJAutoProxy。
spring.aop.proxy-target-class = true #是否要创建基于子类的(CGLIB)代理(true),而不是基于标准Java接口的代理(false)。
#IDENTITY (ContextIdApplicationContextInitializer)
spring.application.name = #Application name。
#DINAND (SpringApplicationAdminJmxAutoConfiguration)
spring.application.admin.enabled = false #是否为应用程序启用管理功能。
spring.application.admin.jmx-name = org.springframework.boot:type = Admin,name = SpringApplication #JMX 应用程序管理员MBean的名称。
#AUTO-CONFIGURATION
spring.autoconfigure.exclude = #要排除的自动配置类。
#BANNER
spring.banner.charset = UTF-8 #横幅文件编码。
spring.banner.location = classpath:banner.txt #横幅文本资源位置。
spring.banner.image.location = classpath:banner.gif #横幅图像文件位置(也可以使用jpg或png)。
spring.banner.image.width = 76 #字符中的横幅图像的宽度。
spring.banner.image.height = #crs 中横幅图像的高度(默认基于图像高度)。
spring.banner.image.margin = 2 #字符中的左手图像边距。
spring.banner.image.invert = false #是否应针对暗终端主题反转图像。
自动配置原理:
1、SpringBoot启动的时候加载主配置类,开始了自动配置功能@EnableAutoConfiguration
2、@EnableAutoConfiguration 的作用:
- 利用EnableAutoConfigurationImportSelector 给容器中导入一些组件
- 可以插件selectImport()方法的内容;
- List configurations = this.getCandidateConfigurations(annotationMetadata, attributes); 获取候选的配置
SpringFactoriesLoader.loadFactoryNames()
扫描所有jar包类路径下 META-INF/spring.factories
把扫描到的这些内容(依赖场景的类文件全路径)包装成properties对象
从properties中获取到EnableAutoConfiguration.class类(全类名)对应的值,然后把他们添加到容器中
将类路径下 META-INF/spring.factories 里面的配置的所有EnableAutoConfiguration 的值加入到了容器中;
# Auto Configure
##EnableAutoConfiguration 类下的所有内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityRequestMatcherProviderAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration
每一个这样的xxxAutoConfiguration 类都是容器中的一个组件,用它们来做自动配置;
3、每一个自动配置类进行自动装配功能;
4、以HttpEncodingAutoConfiguration 为例讲解自动配置原理:
@Configuration//这是一个配置类,一起便携的配置文件一样,也可以给容器中添加组件
@EnableConfigurationProperties({HttpProperties.class})//启动指定类的EnableConfigurationProperties功能;将配置文件中对应的值和HttpEncodingProperties 绑定起来;
@ConditionalOnWebApplication(//spring底层@conditional注解, 根据不同的条件,如果满足指定的条件,整个配置类里面的配置就会生效;判断当前应用是否是web应用;如果是,当前配置类生效
type = Type.SERVLET
)
@ConditionalOnClass({CharacterEncodingFilter.class})//判断当前项目有没有这个类CharacterEncodingFilter;SpringMVC中解决乱码的过滤器;
@ConditionalOnProperty(
prefix = "spring.http.encoding",
value = {"enabled"},
matchIfMissing = true
)//判断配件文件中是否存在某个配置,spring.http.encoding.enabled; 如果不存在;判断也是成立的、及时我们配置文件中不配置(spring.http.encoding.enabled=true),也是默认生效的;
public class HttpEncodingAutoConfiguration {
//它已经和SpringBoot 的配置文件映射了
private final Encoding properties;
//只有一个有参构造函数情况下,参数的值就会从容器中拿
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
@Bean//给容器中添加一个组件,这个组件的某些值需要从properties中获取
@ConditionalOnMissingBean{CharacterEncodingFilter.class})//会根据CharacterEncodingFilter 这个类是否存在来判断这个方法是否生效
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
return filter;
}
根据当前不同的条件判断,决定这个配置类是否生效
一旦这个配置类生效;这个配置类就会给容器中添加各种组件,这些组件的属性值是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;
5、所有的在配置文件中能陪着的属性都是xxxProperties类中的封装者;配置文件能配置什么就可以参照某功能能对应的这个属性类
@ConfigurationProperties(
prefix = "spring.http"
)//从配置文件中获取指定的值和bean的属性进行绑定
public class HttpProperties {
private boolean logRequestDetails;
private final HttpProperties.Encoding encoding = new HttpProperties.Encoding();
精髓:
1、SpringBoot启动会加载大量的自动配置类
2、我们需要的功能有没有SpringBoot默认写好的自动配置类;
3、我们再来看这个自动配置类到底配置了那些组件;(只要自动配置类中的我们需要的组件,我们就不需要再来配置了)
4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中获取这些属性的值。
xxxAutoConfiguration:自动配置类
EnableConfigurationProperties({xxxProperties.class}):启动指定类的EnableConfigurationProperties功能;
给容器中添加组件
xxxProperties:封装配置文件中的属性;
细节:
1、@conditional派生注解(Spring注解版原生的@Conditional作用)
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,被标注的内容才会生效;
@Conditional扩展注解 | 作用(判断是否满足当前指定条件) |
---|---|
@ConditionalOnJava | 系统的Java版本是否符合要求 |
@ConditionalOnBean | 容器中存在指定Bean |
@ConditionalOnMissingBean | 容器中不存在指定Bean |
@ConditionalOnExpression | 满足SqEL表达式 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnMissingClass | 系统没有指定的类 |
@ConditionalOnSingleCandidate | 容器中只有一个指定的Bean,或者这个Bean是首选Bean |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
@ConditionalOnResource | 类路径下是否存在指定资源文件 |
@ConditionalOnWebApplication | 当前是web环境 |
@ConditionalOnNotWebApplication | 当前不是web环境 |
@ConditionalOnJndi | JNDI存在指定项 |
自动配置类必须一定的条件下才能生效
我们怎么知道那些自动配置类生效呢
我们可以通过debug模式启动服务器,可以在控制台打印自动配置报告,这样我们就能知道哪些自动配置类是生效的了;
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches://生效了的配置类
-----------------
CodecsAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition)
CodecsAutoConfiguration.JacksonCodecConfiguration matched:
- @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)
CodecsAutoConfiguration.JacksonCodecConfiguration#jacksonCodecCustomizer matched:
- @ConditionalOnBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found bean 'jacksonObjectMapper' (OnBeanCondition)
DataServiceAutoConfiguration matched:
- @ConditionalOnClass found required class 'cn.zdxh.lcy.demo01.controller.DataService' (OnClassCondition)
- @ConditionalOnProperty (data.enabled) matched (OnPropertyCondition)
..............(后面的省略)
Negative matches://没有生效了的配置类
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition)
ArtemisAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
..............(后面的省略)
SpringBoot与日志
1、日志框架
市面上的日志框架:
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j…
日志门面(日志的抽象层) | 日志实现 |
---|---|
JCL(Jakarta Commons Logging)、slf4j(Simple Logging Facade for java)、Jboss-logging | log4j、JUL(java.util.logging)、 log4j、logback |
我们一般的日志框架就是从左边选一个门面(抽象层)、右边选一个实现;
日志门面:slf4j;
日志实现:logback;
SpringBoot:底层是Spring框架,Spring框架默认是用JCL
SpringBoot选择的是slf4j 和logback
2、slf4j使用
如何在系统中使用slf4j
以后开发的时候,日志记录方法的调用,不应该直接调用日志的实现类,而是调用日志抽象层中的方法给系统里面导入slf4j的jar 和 logback 的实现jar
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerTest {
private static final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
public static void main(String[] args) {
logger.info("Hello World");
}
}
每一个日志的实现都有自己的配置文件,使用slf4j以后,配置文件还是做成日志实现框架自己本身的配置文件;
2、遗留问题
a(slf4j + logback):Spring(commons-logging)、Hibernate(Jboss-logging)、MyBatis、xxx
统一日志记录,即使是别的框架和我们一起使用slf4j进行输出?
如何让系统中所有的日志都统一到slf4j
1、将系统中的其他日志框架先排除出去;
2、用中间包来替换原有的日志框架(适配层)
3、我们导入slf4j其他的实现
SpringBoot的日志框架
<!-- spring boot 场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
SpringBoot使用它来自日志功能
<!-- 日志依赖场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
底层依赖关系:
总结:
1、SpringBoot底层也是使用slf4j + logback 的方式进行日志记录
2、SpringBoot也把其他的日志文件都替换成了slf4j日志框架;
3、中间替换包(适配层)
4、如果我们引入其他框架?一定要把这个框架的默认日志依赖去掉;
Spring框架用的是commons-logging
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-starter</artifactId>
<exclusions>
<exclusion>
<groudId>commons-logging</groudId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
SpringBoot能自动适应所有的日志,而且底层使用slf4j + logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉,在使用中间包(适配层日志jar包)适应本框架下的日志配置。
日志使用
@Test
public void HelloWorld() {
//记录器
Logger logger = LoggerFactory.getLogger(getClass());
//日志的级别
//由低到高 trace<debug<info<warn<error
//可以调整输出的日志级别:日志就只会在这个级别以后的高几倍生效
logger.trace("这是trace日志....");
logger.debug("这是debug日志....");
//SpringBoot默认给我们使用的是info级别的(可以在application.properties配置文件中修改日志权限级别)
//logging.level.com.atguigu=trace(这个设置相当于最高权限,所有的日志都可以输出)
logger.info("这是info日志....");
logger.warn("这是warn日志....");
logger.error("这是error日志....");
}
SpringBoot修改默认日志配置属性
########
#设置日志输出权限级别
logging.level.com.atguigu=trace
##指定日志内容的输出文件夹(不指定当前项目下生成springboot.log日志)可以指定完整路径
logging.file=E:/springboot.log
##在当前项目存放磁盘上的根目录下创建spring文件夹和里面的log文件夹;使用spring.log 默认文件名作为日志内容容器
#loggin.path=spring/log
##在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n
##指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} === [%thread] === %-5level === %logger{50} === -%msg%n
#日志输出格式:
#%d表示日期时间,
#%Thread 表示线程名
#%-5level 表示日志权限级别;从左往右显示5个字符长度
#%logger{50} 表示logger名字最长50个字符,否则按照据点分割。
#%msg 日志消息
%n 是换行符
自定义日志配置
可以通过在类路径中包含适当的库来激活各种日志记录系统,并且可以通过在类路径的根目录中或在以下Spring Environment 属性指定的位置提供合适的配置文件来进一步自定义:logging.config。
您可以使用org.springframework.boot.logging.LoggingSystem系统属性强制Spring Boot使用特定的日志记录系统 。该值应该是实现的完全限定类名LoggingSystem。您还可以通过使用值来完全禁用Spring Boot的日志记录配置none。详情可参考
logback.xml:直接就被日志框架识别了;
logback-spring.xml:日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能
<springProfile name = “staging” >
<! - “暂存”配置文件处于活动状态时启用的配置 - >
</ springProfile>
<springProfile name = “dev | staging” >
<! - 在“dev”或“staging”配置文件处于活动状态时启用的配置 - >
</ springProfile>
<springProfile name = “!production” >
<! - “生产”配置文件未激活时要启用的配置 - >
</ springProfile>
否侧就会报错(找不到日志配置文件)