SpringBoot配置logback的步驟
配置日志文件
spring boot 默認會加載 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。
如需要自定義文件名稱,在 application.properties 中配置 logging.config 選項即可。
在 src/main/resources 下創(chuàng)建 logback-spring.xml 文件,內容如下:
<?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">
<!-- 每天產生一個文件 -->
<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>
<!-- 生產環(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 標簽的 name 屬性對應 application.properties 中的 spring.profiles.active 的配置。
即 spring.profiles.active 的值可以看作是日志配置文件中對應的 springProfile 是否生效的開關。
注解介紹
下面列出 Spring Boot 開發(fā)中常用的注解:
@Configuration # 作用于類上,相當于一個 xml 配置文件 @Bean # 作用于方法上,相當于 xml 配置中的 <bean> @SpringBootApplication # Spring Boot的核心注解,是一個組合注解,用于啟動類上 @EnableAutoConfiguration # 啟用自動配置,允許加載第三方 Jar 包的配置 @ComponentScan # 默認掃描 @SpringBootApplication 所在類的同級目錄以及它的子目錄 @PropertySource # 加載 properties 文件 @Value # 將配置文件的屬性注入到 Bean 中特定的成員變量 @EnableConfigurationProperties # 開啟一個特性,讓配置文件的屬性可以注入到 Bean 中,與 @ConfigurationProperties 結合使用 @ConfigurationProperties # 關聯(lián)配置文件中的屬性到 Bean 中 @Import # 加載指定 Class 文件,其生命周期被 Spring 管理 @ImportResource # 加載 xml 文件
讀取配置文件
屬性裝配
有兩種方式:使用 @Value 注解和 Environment 對象。 在 application.properties 中添加:
ds.userName=root
ds.password=tiger
ds.url=jdbc:mysql://localhost:3306/test
ds.driverClassName=com.mysql.jdbc.Driver
以上是自定義的配置。
創(chuàng)建一個配置類,如下:
@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();
}
}
打印結果:
userName:root
password:tiger
對象裝配
創(chuàng)建一個封裝類: 省略 get set
@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();
}
}
打印結果
ds.url=jdbc:mysql://localhost:3306/test
ds.driverClassName=com.mysql.jdbc.Driver
ds.userName=root
ds.password=tiger
打包運行
打包的形式有兩種:jar 和 war。
jar
默認情況下,通過 maven 執(zhí)行 package 命令后,會生成 jar 包,且該 jar 包會內置了 tomcat 容器,因此我們可以通過 java -jar 就可以運行項目
war
讓 SpringbootApplication 類繼承 SpringBootServletInitializer 并重寫 configure 方法,如下:
@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,如下:
<packaging>war</packaging> 移除內置 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的步驟的詳細內容,更多關于SpringBoot配置logback的資料請關注腳本之家其它相關文章!
- SpringBoot配置lombok與logback過程解析
- springboot配置logback日志管理過程詳解
- 基于logback 實現(xiàn)springboot超級詳細的日志配置
- SpringBoot之LogBack配置詳解
- SpringBoot logback日志框架使用過程解析
- SpringBoot Logback日志記錄到數(shù)據(jù)庫的實現(xiàn)方法
- SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能
- springboot使用logback文件查看錯誤日志過程詳解
- 在SpringBoot中使用Logback管理記錄日志
- SpringBoot之logback-spring.xml不生效的解決方法
- SpringBoot中l(wèi)ogback日志保存到mongoDB的方法
相關文章
Spring中的Schedule動態(tài)添加修改定時任務詳解
這篇文章主要介紹了Spring中的Schedule動態(tài)添加修改定時任務詳解,可能有人會問,為啥不用Quartz,Quartz自然是非常方便強大的,但不是本篇要講的內容,本篇就偏要使用SpringSchedule來實現(xiàn)動態(tài)的cron表達式任務,需要的朋友可以參考下2023-11-11
SpringCloud Gateway 路由配置定位原理分析
本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進行初始化關聯(lián)生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的2021-07-07
Java ArrayList中存放引用數(shù)據(jù)類型的方式
這篇文章主要介紹了Java ArrayList中存放引用數(shù)據(jù)類型的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
在SpringBoot下讀取自定義properties配置文件的方法
這篇文章主要介紹了在SpringBoot下讀取自定義properties配置文件的方法,文中涉及到了Spring-boot中讀取config配置文件的兩種方式,需要的朋友可以參考下2017-12-12

