配置日志文件
spring boot 默認(rèn)會(huì)加載 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。
如需要自定義文件名稱,在 application.properties 中配置 logging.config 選項(xiàng)即可。
在 src/main/resources 下創(chuàng)建 logback-spring.xml 文件,內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > <!-- 文件輸出格式 --> < property name = "PATTERN" value = "%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" /> <!-- test文件路徑 --> < property name = "TEST_FILE_PATH" value = "d:/test.log" /> <!-- pro文件路徑 --> < property name = "PRO_FILE_PATH" value = "/opt/test/log" /> <!-- 開發(fā)環(huán)境 --> < springProfile name = "dev" > < appender name = "CONSOLE" class = "ch.qos.logback.core.ConsoleAppender" > < encoder > < pattern >${PATTERN}</ pattern > </ encoder > </ appender > < logger name = "com.light.springboot" level = "debug" /> < root level = "info" > < appender-ref ref = "CONSOLE" /> </ root > </ springProfile > <!-- 測試環(huán)境 --> < springProfile name = "test" > <!-- 每天產(chǎn)生一個(gè)文件 --> < appender name = "TEST-FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" > <!-- 文件路徑 --> < file >${TEST_FILE_PATH}</ file > < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!-- 文件名稱 --> < fileNamePattern >${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</ fileNamePattern > <!-- 文件最大保存歷史數(shù)量 --> < MaxHistory >100</ MaxHistory > </ rollingPolicy > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern >${PATTERN}</ pattern > </ layout > </ appender > < logger name = "com.light.springboot" level = "debug" /> < root level = "info" > < appender-ref ref = "TEST-FILE" /> </ root > </ springProfile > <!-- 生產(chǎn)環(huán)境 --> < springProfile name = "prod" > < appender name = "PROD_FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file >${PRO_FILE_PATH}</ file > < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > < fileNamePattern >${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</ fileNamePattern > < MaxHistory >100</ MaxHistory > </ rollingPolicy > < layout class = "ch.qos.logback.classic.PatternLayout" > < pattern >${PATTERN}</ pattern > </ layout > </ appender > < root level = "warn" > < appender-ref ref = "PROD_FILE" /> </ root > </ springProfile > </ configuration > |
其中,springProfile 標(biāo)簽的 name 屬性對應(yīng) application.properties 中的 spring.profiles.active 的配置。
即 spring.profiles.active 的值可以看作是日志配置文件中對應(yīng)的 springProfile 是否生效的開關(guān)。
注解介紹
下面列出 Spring Boot 開發(fā)中常用的注解:
1
2
3
4
5
6
7
8
9
10
11
|
@Configuration # 作用于類上,相當(dāng)于一個(gè) xml 配置文件 @Bean # 作用于方法上,相當(dāng)于 xml 配置中的 <bean> @SpringBootApplication # Spring Boot的核心注解,是一個(gè)組合注解,用于啟動(dòng)類上 @EnableAutoConfiguration # 啟用自動(dòng)配置,允許加載第三方 Jar 包的配置 @ComponentScan # 默認(rèn)掃描 @SpringBootApplication 所在類的同級目錄以及它的子目錄 @PropertySource # 加載 properties 文件 @Value # 將配置文件的屬性注入到 Bean 中特定的成員變量 @EnableConfigurationProperties # 開啟一個(gè)特性,讓配置文件的屬性可以注入到 Bean 中,與 @ConfigurationProperties 結(jié)合使用 @ConfigurationProperties # 關(guān)聯(lián)配置文件中的屬性到 Bean 中 @Import # 加載指定 Class 文件,其生命周期被 Spring 管理 @ImportResource # 加載 xml 文件 |
讀取配置文件
屬性裝配
有兩種方式:使用 @Value 注解和 Environment 對象。 在 application.properties 中添加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
ds.userName=root ds.password=tiger ds.url=jdbc:mysql: //localhost:3306/test ds.driverClassName=com.mysql.jdbc.Driver 以上是自定義的配置。 創(chuàng)建一個(gè)配置類,如下: @Configuration public class WebConfig { @Value ( "${ds.userName}" ) private String userName; @Autowired private Environment environment; public void show() { System.out.println( "ds.userName:" + this .userName); System.out.println( "ds.password:" + this .environment.getProperty( "ds.password" )); } } 通過 @Value 獲取 config.userName 配置;通過 environment 獲取 config.password 配置。 測試: @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication. class , args); context.getBean(WebConfig. class ).show(); } } 打印結(jié)果: userName:root password:tiger |
對象裝配
創(chuàng)建一個(gè)封裝類: 省略 get set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@Component @ConfigurationProperties (prefix= "ds" ) public class DataSourceProperties { private String url; private String driverClassName; private String userName; private String password; public void show() { System.out.println( "ds.url=" + this .url); System.out.println( "ds.driverClassName=" + this .driverClassName); System.out.println( "ds.userName=" + this .userName); System.out.println( "ds.password=" + this .password); } } 測試: @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication. class , args); context.getBean(DataSourceProperties. class ).show(); } } 打印結(jié)果 ds.url=jdbc:mysql: //localhost:3306/test ds.driverClassName=com.mysql.jdbc.Driver ds.userName=root ds.password=tiger |
打包運(yùn)行
打包的形式有兩種:jar 和 war。
jar
默認(rèn)情況下,通過 maven 執(zhí)行 package 命令后,會(huì)生成 jar 包,且該 jar 包會(huì)內(nèi)置了 tomcat 容器,因此我們可以通過 java -jar 就可以運(yùn)行項(xiàng)目
war
讓 SpringbootApplication 類繼承 SpringBootServletInitializer 并重寫 configure 方法,如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
@SpringBootApplication public class SpringbootApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootApplication. class ); } public static void main(String[] args) { SpringApplication.run(SpringbootApplication. class , args); } } |
修改 pom.xml 文件,將 jar 改成 war,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< packaging >war</ packaging > 移除內(nèi)置 tomcat: < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > < exclusions > < exclusion > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-tomcat</ artifactId > </ exclusion > </ exclusions > </ dependency > <!-- Servlet API --> < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > < version >3.1.0</ version > < scope >provided</ scope > </ dependency > |
以上就是SpringBoot配置logback的步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot配置logback的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://cloud.tencent.com/developer/article/1639939