接昨天的
4、如果要引入其他框架,首先要移除这些框架所依赖的日志jar
> springboot能够自动适配所有的日志,而且底层使用的是SLF4j+logback的方式记录,如果要引入其他框架首先要把引入的框架日志框架移除,换成中间包
日志的使用:
|logging.file|logging.path|Example|Description|
|:--:|:--:|:--:|:--:|
|不指定|不指定|无|只在控制台输出|
|指定文件名|不指定|my.log|输出日志到my.log文件|
|不指定|指定目录|/var/log|输出到指定目录下的spring.log|
ps:指定说的是在配置文件中进行指定(application.properties/yaml)
```properties
logging.level.com.asher=trace
#在当前项目下生成springboot.log日志文件,也可以指定路径
#logging.file=C:/Users/asher/Desktop/springboot.log
#在当前磁盘的根路径下创建spring文件夹下创建log文件夹,使用默认的spring.log文件接收日志
logging.path=/spring/log
#%d表示日期时间
#%thread表示线程名
#%-5level:级别从左显示5个字符
#%logger{50} 表示logger名字最长50个字符,否则按照句点分割
#%msg:日志消息
#%n是换行符
#指定在控制台输出的格式
logging.pattern.console=>%d{yyyy-MM-dd HH:mm:ss:SS} [%thread] %-5level %logger{50} - %msg%n
#指定在文件中输出的格式
logging.pattern.file=>%d{yyyy-MM-dd HH:mm:ss:SS} [%thread] %-5level %logger{50} - %msg%n
```
默认格式路径


logback.xml配置在和application.properties同目录下可以被直接识别并替换之前默认配置。
logback-spring.xml:日志框架就不直接加载日志的配置项了,由SpringBoot解析日志配置,可以使用Spring Boot的高级profile功能,只需要在application.properties/yml中指定
spring.profiles.active=[profileName]
下面的代码添加进logback-spring.xml中
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
使用SpringBoot
: 1、创建Spring Boot应用,选择需要的模块
2、Spring Boot就已经将这些默认场景配置好了,只需要配置文件中指定的少量配置就可以运行起来
3、编写业务逻辑代码:
***AutoConfigguration:帮助我们给容器中自动配置组件
***properties:配置类来封装配置文件的内容
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknowFields = false 可以设置和静态资源有关的参数,缓存时间等等
)
SpringBoot对静态资源的映射规则
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
1、所有的webjars/**,都去META-INF/resources/webjars找资源


<font color="red">localhost:8080/webjars/jquery/3.3.1-2/jquery.js可以直接访问到静态资源</font>

2、/**访问当前项目的任何资源(静态资源的文件夹),
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/" 当前项目的类路径
指图片里的路径

例如:
下面的这个静态资源可以直接在浏览器访问到


欢迎页
也就是静态资源文件夹下所有的index.html页面;被/**访问
localhost:8080/找静态资源下找index.html
//配置首页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}
4、所有的<font color="green">**/favicon.ico</font>都是在静态资源下查找
指定静态资源文件夹
spring.resources.static-locations=classpath:/hello/,classpath:/redpig/
网友评论