主要包含日誌(日誌框架和日誌配置)web開發(簡介和對靜態資源的映射規則)
日誌(日誌框架,日誌配置)
1、日誌框架
市面上的日誌框架;
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j…
日誌門面 (日誌的抽象層) | 日誌實現 |
---|---|
Log4j JUL(java.util.logging) Log4j2 Logback |
左邊選一個門面(抽象層)、右邊來選一個實現;
日誌門面: SLF4J;
日誌實現:Logback;
SpringBoot:底層是Spring框架,Spring框架默認是用JCL;‘
SpringBoot選用 SLF4j和logback;
2、SLF4j使用
1、如何在系統中使用SLF4j https://www.slf4j.org
以後開發的時候,日誌記錄方法的調用,不應該來直接調用日誌的實現類,而是調用日誌抽象層裏面的方法;
給系統裏面導入slf4j的jar和 logback的實現jar
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");
}
}
圖示:
[外鏈圖片轉存失敗(img-AZGRBTNF-1568107666712)(http://47.94.208.193/wp-content/uploads/2019/07/1-2-300x166.png)]
每一個日誌的實現框架都有自己的配置文件。使用slf4j以後,配置文件還是做成日誌實現框架自己本身的配置文件;
2、遺留問題
a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx
統一日誌記錄,即使是別的框架也統一使用slf4j進行輸出。
[外鏈圖片轉存失敗(img-mDGEFv0k-1568107666713)(http://47.94.208.193/wp-content/uploads/2019/07/2-1-300x212.png)]
如何讓系統中所有的日誌都統一到slf4j;
1、將系統中其他日誌框架先排除出去;
2、用中間包來替換原有的日誌框架;
3、我們導入slf4j其他的實現
3、SpringBoot日誌關係
<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)、中間替換包
@SuppressWarnings("rawtypes")
public abstract class LogFactory {
static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";
static LogFactory logFactory = new SLF4JLogFactory();
[外鏈圖片轉存失敗(img-ZymIdTg1-1568107666715)(http://47.94.208.193/wp-content/uploads/2019/07/4-300x154.png)]
4)、如果我們要引入其他框架,一定要把這個框架的默認日誌依賴移除掉。
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>
SpringBoot能自動適配所有的日誌,而且底層使用slf4j+logback的方式記錄日誌,引入其他框架的時候,只需要把這個框架依賴的日誌框架排除掉即可;
4、日誌使用;
1、默認配置
SpringBoot默認幫我們配置好了日誌;
//記錄器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
//System.out.println();
//日誌的級別;
//由低到高 trace<debug<info<warn<error
//可以調整輸出的日誌級別;日誌就只會在這個級別以以後的高級別生效
logger.trace("這是trace日誌...");
logger.debug("這是debug日誌...");
//SpringBoot默認給我們使用的是info級別的,沒有指定級別的就用SpringBoot默認規定的級別;root級別
logger.info("這是info日誌...");
logger.warn("這是warn日誌...");
logger.error("這是error日誌...");
}
日誌輸出格式:
%d表示日期時間,
%thread表示線程名,
%-5level:級別從左顯示5個字符寬度
%logger{50} 表示logger名字最長50個字符,否則按照句點分割。
%msg:日誌消息,
%n是換行符
-->
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
SpringBoot修改日誌的默認配置
logging.level.com.atguigu=trace
#logging.path=
# 不指定路徑在當前項目下生成springboot.log日誌
# 可以指定完整的路徑;
#logging.file=G:/springboot.log
# 在當前磁盤的根路徑下創建spring文件夾和裏面的log文件夾;使用 spring.log 作爲默認文件
logging.path=/spring/log
# 在控制檯輸出的日誌的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# 指定文件中日誌輸出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
logging.file | logging.path | Example | Description |
---|---|---|---|
(none) | (none) | 只在控制檯輸出 | |
指定文件名 | (none) | my.log | 輸出日誌到my.log文件 |
(none) | 指定目錄 | /var/log | 輸出到指定目錄的 spring.log 文件中 |
2、指定配置
給類路徑下放上每個日誌框架自己的配置文件即可;SpringBoot就不使用他默認配置的了
Logging System | Customization |
---|---|
Logback | logback-spring.xml , logback-spring.groovy , logback.xml or logback.groovy |
Log4j2 | log4j2-spring.xml or log4j2.xml |
JDK (Java Util Logging) | logging.properties |
logback.xml:直接就被日誌框架識別了;
logback-spring.xml:日誌框架就不直接加載日誌的配置項,由SpringBoot解析日誌配置,可以使用SpringBoot的高級Profile功能
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
可以指定某段配置只在某個環境下生效
</springProfile>
如:
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!--
日誌輸出格式:
%d表示日期時間,
%thread表示線程名,
%-5level:級別從左顯示5個字符寬度
%logger{50} 表示logger名字最長50個字符,否則按照句點分割。
%msg:日誌消息,
%n是換行符
-->
<layout class="ch.qos.logback.classic.PatternLayout">
<springProfile name="dev">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%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>
如果使用logback.xml作爲日誌配置文件,還要使用profile功能,會有以下錯誤
no applicable action for [springProfile]
5、切換日誌框架
可以按照slf4j的日誌適配圖,進行相關的切換;
slf4j+log4j的方式;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
切換爲log4j2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Web開發
1、簡介
使用SpringBoot;
1)、創建SpringBoot應用,選中我們需要的模塊;
2)、SpringBoot已經默認將這些場景配置好了,只需要在配置文件中指定少量配置就可以運行起來
3)、自己編寫業務代碼;
*自動配置原理?
xxxxAutoConfiguration:幫我們給容器中自動配置組件;
xxxxProperties:配置類來封裝配置文件的內容;
2、SpringBoot對靜態資源的映射規則;
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
//可以設置和靜態資源有關的參數,緩存時間等
1、WebMvcAuotConfiguration:Spring MVC的相關配置
WebMvcAuotConfiguration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(
registry.addResourceHandler("/webjars/**")
.addResourceLocations(
"classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
//靜態資源文件夾映射
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(
registry.addResourceHandler(staticPathPattern)
.addResourceLocations(
this.resourceProperties.getStaticLocations())
.setCachePeriod(cachePeriod));
}
}
//配置歡迎頁映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ResourceProperties resourceProperties) {
return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
//配置喜歡的圖標
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration {
private final ResourceProperties resourceProperties;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//所有 **/favicon.ico
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler
.setLocations(this.resourceProperties.getFaviconLocations());
return requestHandler;
}
}
詳解:
1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找資源;
webjars:以jar包的方式引入靜態資源;參考http://www.webjars.org/在其中以Maven依賴的方式引入。
例子:
[外鏈圖片轉存失敗(img-mH7gCwrc-1568107666716)(http://47.94.208.193/wp-content/uploads/2019/07/5-300x188.png)]
localhost:8080/webjars/jquery/3.3.1/jquery.js
[外鏈圖片轉存失敗(img-rBjsax3x-1568107666716)(http://47.94.208.193/wp-content/uploads/2019/07/6-300x164.png)]
<!--引入jquery-webjar-->在訪問的時候只需要寫webjars下面資源的名稱即可
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
2)、"/**" 訪問當前項目的任何資源,都去(靜態資源的文件夾)找映射
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當前項目的根路徑
localhost:8080/abc === 去靜態資源文件夾裏面找abc
例子
[外鏈圖片轉存失敗(img-zMedCyf5-1568107666717)(http://47.94.208.193/wp-content/uploads/2019/07/8-300x271.png)]
http://localhost:8080/asserts/js/Chart.min.js
[外鏈圖片轉存失敗(img-8LYs2cmr-1568107666718)(http://47.94.208.193/wp-content/uploads/2019/07/7-300x179.png)]
3)、歡迎頁; 靜態資源文件夾下的所有index.html頁面;被"/**"映射;
localhost:8080/ 找index頁面
4)、所有的 **/favicon.ico 都是在靜態資源文件下找;
例子
[外鏈圖片轉存失敗(img-hBnIp62D-1568107666721)(http://47.94.208.193/wp-content/uploads/2019/07/10-300x252.png)]
要想改變靜態文件夾的路徑就在配置文件中設置:
spring.resources.static-locations=classpatg:/hello,classpath:/inspur/
#因爲其是用數組存儲的所以用逗號分隔。