轉自http://blog.csdn.net/clementad/article/details/51498864
在spring boot中,配置log4j2的幾個步驟(使用yml文件):
1、pom文件的依賴配置中,去掉spring boot默認的log配置,引入log4j2依賴包:
- <!-- log related -->
- <dependency> <!-- exclude掉spring-boot的默認log配置 -->
- <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> <!-- 引入log4j2依賴 -->
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-log4j2</artifactId>
- </dependency>
- <dependency> <!-- 加上這個才能辨認到log4j2.yml文件 -->
- <groupId>com.fasterxml.jackson.dataformat</groupId>
- <artifactId>jackson-dataformat-yaml</artifactId>
- </dependency>
- <!-- end of log related -->
2、log4j2.yml文件配置(放置在resources文件夾中):
- Configuration:
- status: warn
- Properties: # 定義全局變量
- Property: # 缺省配置(用於開發環境)。其他環境需要在VM參數中指定,如下:
- #測試:-Dlog.level.console=warn -Dlog.level.xjj=trace
- #生產:-Dlog.level.console=warn -Dlog.level.xjj=info
- - name: log.level.console
- value: trace
- - name: log.level.xjj
- value: trace
- - name: log.path
- value: /opt/logs
- - name: project.name
- value: my-spring-boot
- Appenders:
- Console: #輸出到控制台
- name: CONSOLE
- target: SYSTEM_OUT
- ThresholdFilter:
- level: ${sys:log.level.console} # “sys:”表示:如果VM參數中沒指定這個變量值,則使用本文件中定義的缺省全局變量值
- onMatch: ACCEPT
- onMismatch: DENY
- PatternLayout:
- pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
- RollingFile: # 輸出到文件,超過128MB歸檔
- - name: ROLLING_FILE
- ignoreExceptions: false
- fileName: ${log.path}/${project.name}.log
- filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
- PatternLayout:
- pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
- Policies:
- SizeBasedTriggeringPolicy:
- size: "128 MB"
- DefaultRolloverStrategy:
- max: 1000
- Loggers:
- Root:
- level: info
- AppenderRef:
- - ref: CONSOLE
- - ref: ROLLING_FILE
- Logger: # 為com.xjj包配置特殊的Log級別,方便調試
- - name: com.xjj
- additivity: false
- level: ${sys:log.level.xjj}
- AppenderRef:
- - ref: CONSOLE
- - ref: ROLLING_FILE
3、測試用例:
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = MySpringBootApplication.class)
- public class MySpringBootApplicationTests {
- protected final Logger logger = LoggerFactory.getLogger(this.getClass());
- protected final ObjectMapper objectMapper = new ObjectMapper();
- @Test
- public void contextLoads() {
- logger.trace("I am trace log.");
- logger.debug("I am debug log.");
- logger.warn("I am warn log.");
- logger.error("I am error log.");
- }
- }
測試結果:
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v1.3.5.RELEASE)
- 2016-05-25 15:36:30,245:INFO main (StartupInfoLogger.java:48) - Starting MySpringBootApplicationTests on WIN-UCBBGRHGRK9 with PID 27276 (C:\workspace-sts\my-spring-boot\target\test-classes started by Xu in C:\workspace-sts\my-spring-boot)
- 2016-05-25 15:36:30,245:DEBUG main (StartupInfoLogger.java:51) - Running with Spring Boot v1.3.5.RELEASE, Spring v4.2.6.RELEASE
- 2016-05-25 15:36:30,245:INFO main (SpringApplication.java:666) - No active profile set, falling back to default profiles: default
- 2016-05-25 15:36:30,285:INFO main (AbstractApplicationContext.java:578) - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@638ef7ed: startup date [Wed May 25 15:36:30 CST 2016]; root of context hierarchy
- 2016-05-25 15:36:30,522:INFO background-preinit (Version.java:30) - HV000001: Hibernate Validator 5.2.4.Final
- 2016-05-25 15:36:31,673:INFO main (StartupInfoLogger.java:57) - Started MySpringBootApplicationTests in 1.726 seconds (JVM running for 2.749)
- 2016-05-25 15:36:31,681:TRACE main (MySpringBootApplicationTests.java:20) - I am trace log.
- 2016-05-25 15:36:31,681:DEBUG main (MySpringBootApplicationTests.java:21) - I am debug log.
- 2016-05-25 15:36:31,682:WARN main (MySpringBootApplicationTests.java:22) - I am warn log.
- 2016-05-25 15:36:31,682:ERROR main (MySpringBootApplicationTests.java:23) - I am error log.
- 2016-05-25 15:36:31,689:INFO Thread-1 (AbstractApplicationContext.java:960) - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@638ef7ed: startup date [Wed May 25 15:36:30 CST 2016]; root of context hierarchy
可以看到4個log都出來了,調整log.level參數可以決定讓哪個級別的log出來。
同時,硬盤中/opt/logs/目錄下也自動生成了一個叫做my-spring-boot.log的文件,保存了相同的log內容。
詳細的源代碼參考:https://github.com/xujijun/my-spring-boot
