Java笔记-Log4j在Spring Boot中的使用
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-Log4j在Spring Boot中的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在Java企業(yè)級開發(fā)中都需要日志記錄相關(guān)的操作。
Maven相關(guān)配置如下,首先要去除Spring Boot自帶的日志,然后添加log4j2的日志:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId> </dependency>Gradle依賴:
dependencies {compile 'org.springframework.boot:spring-boot-starter-log4j2' } configurations {all {exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'} }?
Spring Boot中關(guān)于Log4J2在4個地址配置能生效:
1. XML配置(默認(rèn)采用的配置)
2. JSON
3. YAML
4. Properties文件
只要這4個文件在環(huán)境變量里面,程序都會讀其進行讀取對應(yīng)的文件分別是:
log4j2.xml
log4j2.json
log4j2.properties
log4j2.yaml
在項目的資源文件中添加log4j2.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"><Properties><Property name="PID">????</Property><Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property></Properties><Appenders><Console name="Console" target="SYSTEM_OUT" follow="true"><PatternLayout pattern="${LOG_PATTERN}"/></Console></Appenders><Loggers><Logger name="com.javadevjournal" level="debug" additivity="false"><AppenderRef ref="Console" /></Logger><Root level="info"><AppenderRef ref="Console"/></Root></Loggers> </Configuration>這樣Log4j2就配置好了。
?
測試的時候,簡單寫一個restful接口:
Log4J2Controller.java
@RestController public class Log4J2Controller {private static final Logger LOG = LogManager.getLogger(Log4J2Controller.class);@GetMapping("/test")public String test(){LOG.debug("Debugging Debugging Debugging Debugging");LOG.info("Info Info Info Info Info Info Info Info");LOG.warn("Warn Warn Warn Warn Warn Warn Warn Warn");LOG.error("Error Error Error Error Error Error Error");LOG.fatal("Fatal Fatal Fatal Fatal Fatal Fatal Fatal");return "Hello Test";} }打印如下:
源碼打包下載:
https://github.com/fengfanchen/Java/tree/master/Log4j2Demo
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Java笔记-Log4j在Spring Boot中的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-spring-rabbit
- 下一篇: java美元兑换,(Java实现) 美元