springboot引入log4j2
1、导入依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
2、yml配置文件
Configuration:
status: warn
Properties: # 定义全局变量
Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
- name: log.path
value: G:/log/logs
- name: project.name
value: project_name
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
PatternLayout:
pattern: "%highlight{%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t %l - %m%n}{STYLE=Logback}"
RollingFile:
- 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:
- name: com.xxx #给自己项目配置特定的日志级别和appender
additivity: false
level: debug
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
3、springboot默认使用logback来输出日志,只做以上配置,log4j是不会起作用的,控制台会输出以下错误提示
maven中加入一下配置,解决日志冲突
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <!-- 解决logback 日志冲突 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
4、测试
@PostMapping(value = "login") @ResponseBody public Result login(@Valid Users users){ logger.debug("进入登录接口..."); Subject subject = SecurityUtils.getSubject(); AuthenticationToken token = new UsernamePasswordToken(users.getUsername(),users.getPassword()); subject.login(token); return Result.buildBaseSuccess(); }
5、配置切面,为每个接口添加通用日志
@Aspect @Component @Order(1) public class WebLogAspect { private Logger logger = LoggerFactory.getLogger(this.getClass()); private ThreadLocal<Long> startTime = new ThreadLocal<>(); @Pointcut(value = "execution(public * com.*.controller.*.*(..))") public void webLog() { } @Before(value = "webLog()") public void dobefore(JoinPoint joinPoint) { //注入连接点 startTime.set(System.currentTimeMillis()); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); logger.info("请求URL:" + request.getRequestURL().toString()); // 想那个url发的请求 logger.info("请求METHOD:" + request.getMethod()); logger.info("请求方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); // 请求的是哪个类,哪种方法 logger.info("请求方法参数:" + JSONObject.toJSONString(joinPoint.getArgs())); // 方法本传了哪些参数 logger.info("请求request参数:" + JSONObject.toJSONString(joinPoint.getArgs())); // 方法本传了哪些参数 } //方法的返回值注入给ret @AfterReturning(returning = "ret", pointcut = "webLog()") public void doafter(Object ret) { logger.info("返回结果:" + JSONObject.toJSONString(ret)); // 响应的内容---方法的返回值responseEntity logger.info("用时:" + ( System.currentTimeMillis()-startTime.get() )); } }
6、效果