**官方说明:**Apache Log4j 2
是对 Log4j 的升级,并提供了许多 Logback 可用的改进,同时解决了 Logback 体系结构中的一些固有问题。
官网:http://logging.apache.org/log4j/2.x/
排除Logback依赖
Spring Boot默认已经集成 Logback 日志工具,要使用 Log4j2必须先排除 Logback。
1 2 3 4 5 6 7 8 9 10 11
| <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>
|
引入Log4j2依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
|
配置Log4j2.xml文件
创建log4j2.xml
文件,放在工程resources
目录里。
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
| <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30"> <Properties> <Property name="log.path.root">logs</Property> <Property name="log.file">project_name</Property> <Property name="log.pattern">%d %p [%C{1.}->%M:%L] [%t] %m%n</Property> </Properties>
<Appenders> <Console name="Console" target="SYSTEM_OUT" follow="true"> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout> <charset>UTF-8</charset> <pattern>${log.pattern}</pattern> </PatternLayout> </Console>
<RollingFile name="INFO-LOG" fileName="${log.path.root}/info/${log.file}.log" immediateFlush="false" filePattern="${log.path.root}/info/${log.file}-%d{yyyy-MM-dd}_%i.log.gz">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${log.pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="24"/> </Policies> </RollingFile>
<RollingFile name="ERROR-LOG" fileName="${log.path.root}/error/${log.file}.log" filePattern="${log.path.root}/info/${log.file}-%d{yyyy-MM-dd}_%i.log.gz"> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="${log.pattern}"/> <Policies> <TimeBasedTriggeringPolicy modulate="true" interval="1"/> </Policies> </RollingFile> </Appenders>
<Loggers> <logger name="jdbc.sqlonly" level="INFO"/> <logger name="jdbc.sqltiming" level="OFF"/> <logger name="jdbc.resultsettable" level="INFO"/> <logger name="jdbc.resultset" level="OFF"/> <logger name="jdbc.connection" level="OFF"/> <logger name="jdbc.audit" level="OFF"/> <Root level="INFO"> <AppenderRef ref="Console"/> <AppenderRef ref="INFO-LOG"/> <AppenderRef ref="ERROR-LOG"/> </Root> </Loggers>
</Configuration>
|
关于Log4j2
的配置,详解见Log:log4j2.xml 配置示例和异步日志详解 。
调用Logger
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
| import com.alibaba.fastjson.JSON; import com.springboot.example.entity.User; import com.springboot.example.service.UserService;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping(value = "/user",method = RequestMethod.POST) public class UserController { private static final Logger logger = LogManager.getLogger(UserController.class);
@Autowired private UserService userService;
@RequestMapping("/query") public User addUser(User user){ user.setAddress("中国").setAge(11).setName("Kitty"); logger.info("user:{}", JSON.toJSONString(user)); int i = userService.addUser(user); return user; } }
|
logger.info输入示例:city:{“city”:”Hamilton”,”cityId”:200,”countryId”:68,”lastUpdate”:1139949925000}