SpringBoot配置logback的步驟
配置日志文件
spring boot 默認(rèn)會加載 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。
如需要自定義文件名稱,在 application.properties 中配置 logging.config 選項即可。
在 src/main/resources 下創(chuàng)建 logback-spring.xml 文件,內(nèi)容如下:
<?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)生一個文件 --> <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ā)中常用的注解:
@Configuration # 作用于類上,相當(dāng)于一個 xml 配置文件 @Bean # 作用于方法上,相當(dāng)于 xml 配置中的 <bean> @SpringBootApplication # Spring Boot的核心注解,是一個組合注解,用于啟動類上 @EnableAutoConfiguration # 啟用自動配置,允許加載第三方 Jar 包的配置 @ComponentScan # 默認(rèn)掃描 @SpringBootApplication 所在類的同級目錄以及它的子目錄 @PropertySource # 加載 properties 文件 @Value # 將配置文件的屬性注入到 Bean 中特定的成員變量 @EnableConfigurationProperties # 開啟一個特性,讓配置文件的屬性可以注入到 Bean 中,與 @ConfigurationProperties 結(jié)合使用 @ConfigurationProperties # 關(guān)聯(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(); } } 打印結(jié)果: 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(); } } 打印結(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 命令后,會生成 jar 包,且該 jar 包會內(nèi)置了 tomcat 容器,因此我們可以通過 java -jar 就可以運(yùn)行項目
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> 移除內(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)注腳本之家其它相關(guān)文章!
- SpringBoot配置lombok與logback過程解析
- springboot配置logback日志管理過程詳解
- 基于logback 實(shí)現(xiàn)springboot超級詳細(xì)的日志配置
- SpringBoot之LogBack配置詳解
- SpringBoot logback日志框架使用過程解析
- SpringBoot Logback日志記錄到數(shù)據(jù)庫的實(shí)現(xiàn)方法
- SpringBoot+Logback實(shí)現(xiàn)一個簡單的鏈路追蹤功能
- springboot使用logback文件查看錯誤日志過程詳解
- 在SpringBoot中使用Logback管理記錄日志
- SpringBoot之logback-spring.xml不生效的解決方法
- SpringBoot中l(wèi)ogback日志保存到mongoDB的方法
相關(guān)文章
elasticsearch索引index之Translog數(shù)據(jù)功能分析
這篇文章主要為大家介紹了elasticsearch索引index之Translog數(shù)據(jù)功能分析,主要分析translog的結(jié)構(gòu)及寫入方式,有需要的朋友可以借鑒參考下2022-04-04cookie+mybatis+servlet實(shí)現(xiàn)免登錄時長兩天半的整體流程
這篇文章主要介紹了cookie+mybatis+servlet實(shí)現(xiàn)免登錄時長兩天半,主要用到的技術(shù)有session、cookie、轉(zhuǎn)發(fā)、重定向、filter、和servlet,最重要的還是具體的來運(yùn)用它們在前端頁面真正的搭建出一個應(yīng)用,通過這個練習(xí),對我們所學(xué)的web知識做一個整合,需要的朋友可以參考下2022-10-10SpringBoot?@Value與@ConfigurationProperties二者有哪些區(qū)別
這篇文章主要介紹了SpringBoot?@Value與@ConfigurationProperties二者的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10淺談Storm在zookeeper上的目錄結(jié)構(gòu)
這篇文章主要介紹了淺談Storm在zookeeper上的目錄結(jié)構(gòu)的相關(guān)內(nèi)容,涉及storm使用zookeeper的操作以及詳細(xì)結(jié)構(gòu)圖,具有一定參考價值,需要的朋友可以了解下。2017-10-10Java隨機(jī)生成驗(yàn)證碼的實(shí)現(xiàn)示例
這篇文章主要介紹Java隨機(jī)生成驗(yàn)證碼的實(shí)現(xiàn)方法,文中有相關(guān)的實(shí)現(xiàn)代碼供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-08-08springboot+mybatis通過實(shí)體類自動生成數(shù)據(jù)庫表的方法
這篇文章主要介紹了springboot+mybatis通過實(shí)體類自動生成數(shù)據(jù)庫表的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07使用Springboot根據(jù)配置文件動態(tài)注入接口實(shí)現(xiàn)類
這篇文章主要介紹了使用Springboot根據(jù)配置文件動態(tài)注入接口實(shí)現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08淺談spring中的default-lazy-init參數(shù)和lazy-init
下面小編就為大家?guī)硪黄獪\談spring中的default-lazy-init參數(shù)和lazy-init。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04