一 本章概述
SpringBoot应用替换默认的Logback日之框架,采用Log4j2实现
二 SpringBoot应用集成Log4j2
1 替换默认的logback
- <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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2 加入基于SpringBoot的Log4j2 Starter
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-log4j2</artifactId>
- </dependency>
- 1
- 2
- 3
- 4
3 配置log4j2.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!--设置log4j2的自身log级别为warn -->
- <configuration status="warn">
- <properties>
- <Property name="app_name">springboot-web</Property>
- <Property name="log_path">logs/${app_name}</Property>
- </properties>
- <appenders>
- <console name="Console" target="SYSTEM_OUT">
- <PatternLayout pattern="[%d][%t][%p][%l] %m%n" />
- </console>
- <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"
- filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
- <Filters>
- <ThresholdFilter level="INFO" />
- <ThresholdFilter level="WARN" onMatch="DENY"
- onMismatch="NEUTRAL" />
- </Filters>
- <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
- <Policies>
- <!-- 归档每天的文件 -->
- <TimeBasedTriggeringPolicy interval="1" modulate="true" />
- <!-- 限制单个文件大小 -->
- <SizeBasedTriggeringPolicy size="2 MB" />
- </Policies>
- <!-- 限制每天文件个数 -->
- <DefaultRolloverStrategy compressionLevel="0" max="10"/>
- </RollingFile>
- <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"
- filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
- <Filters>
- <ThresholdFilter level="WARN" />
- <ThresholdFilter level="ERROR" onMatch="DENY"
- onMismatch="NEUTRAL" />
- </Filters>
- <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
- <Policies>
- <!-- 归档每天的文件 -->
- <TimeBasedTriggeringPolicy interval="1" modulate="true" />
- <!-- 限制单个文件大小 -->
- <SizeBasedTriggeringPolicy size="2 MB" />
- </Policies>
- <!-- 限制每天文件个数 -->
- <DefaultRolloverStrategy compressionLevel="0" max="10"/>
- </RollingFile>
- <RollingFile name="RollingFileError" fileName="${log_path}/error.log"
- filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
- <ThresholdFilter level="ERROR" />
- <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
- <Policies>
- <!-- 归档每天的文件 -->
- <TimeBasedTriggeringPolicy interval="1" modulate="true" />
- <!-- 限制单个文件大小 -->
- <SizeBasedTriggeringPolicy size="2 MB" />
- </Policies>
- <!-- 限制每天文件个数 -->
- <DefaultRolloverStrategy compressionLevel="0" max="10"/>
- </RollingFile>
- <!-- 配置mongdb appender -->
- </appenders>
- <loggers>
- <!--过滤掉spring和hibernate的一些无用的debug信息 -->
- <root level="info">
- <appender-ref ref="Console" />
- <appender-ref ref="RollingFileInfo" />
- <appender-ref ref="RollingFileWarn" />
- <appender-ref ref="RollingFileError" />
- <!-- 输出日志到mongodb -->
- </root>
- </loggers>
- </configuration>
- 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
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
4 配置appliaction.properties
logging.config=classpath:log4j2.xml
- 1
三 Log4j2的应用
- package com.ekeyfund.framework.springboot.web.controller;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * IndexController
- *
- * @author tony 18601767221@163.com
- * @create 2017-09-14-下午4:51
- * @see
- * @since JDK1.8u141
- */
- @RestController
- public class IndexController {
- private static final Logger logger = LogManager.getLogger(IndexController.class);
- @GetMapping("/index")
- public String index(){
- for(int i=0;i<10_0000;i++){
- logger.info("info execute index method");
- logger.warn("warn execute index method");
- logger.error("error execute index method");
- }
- return "My First SpringBoot Application";
- }
- }
- 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