Spring Boot学习之五日志管理

Posted cac2020

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot学习之五日志管理相关的知识,希望对你有一定的参考价值。

环境
  eclipse 4.7
  jdk 1.8
  Spring Boot 1.5.2

一、log4j

常见方式:log4j.properties + org.apache.log4j.Logger
比如:
log4j.properties:

log4j.rootLogger=info,error,CONSOLE,DEBUG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
log4j.appender.CONSOLE.layout.ConversionPattern=%dyyyy-MM-dd-HH-mm [%t] [%c] [%p] - %m%n     

log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout     
log4j.appender.info.layout.ConversionPattern=%dyyyy-MM-dd-HH-mm [%t] [%c] [%p] - %m%n  
log4j.appender.info.datePattern=‘.‘yyyy-MM-dd
log4j.appender.info.Threshold = info   
log4j.appender.info.append=true   
#log4j.appender.info.File=/home/admin/pms-api-services/logs/info/api_services_info
log4j.appender.info.File=../logs/info/api_services_info

log4j.logger.error=error  
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout     
log4j.appender.error.layout.ConversionPattern=%dyyyy-MM-dd-HH-mm [%t] [%c] [%p] - %m%n  
log4j.appender.error.datePattern=‘.‘yyyy-MM-dd
log4j.appender.error.Threshold = error   
log4j.appender.error.append=true   
#log4j.appender.error.File=/home/admin/pms-api-services/logs/error/api_services_error
log4j.appender.error.File=../logs/error/api_services_error

log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
log4j.appender.DEBUG.layout.ConversionPattern=%dyyyy-MM-dd-HH-mm [%t] [%c] [%p] - %m%n  
log4j.appender.DEBUG.datePattern=‘.‘yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG   
log4j.appender.DEBUG.append=true   
#log4j.appender.DEBUG.File=/home/admin/pms-api-services/logs/debug/api_services_debug
log4j.appender.DEBUG.File=../logs/debug/api_services_debug

controller:

package com.wjy.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wjy.test1.service.UserServiceTest1;

@RestController
public class UserController 

    private static Logger log = Logger.getLogger(UserController.class);
    
    @Autowired
    public UserServiceTest1 userServiceTest1;
    
    @RequestMapping("/insertTest1ByService")
    public String insertTest1ByService(String name,Integer age) 
        log.info("insertTest1ByService:"+name+","+age);
        
        userServiceTest1.insertuser1(name, age);
        return "success";
    
    
    

二、AOP 切面记录日志
1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2、切面

package com.wjy.aop;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class WebLogAspect 
    private static Logger logger = Logger.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.wjy.controller..*.*(..))")
    public void weblog() 
        //具体业务处理  比如报文入表等等
        //logger.info("weblog(.....)");
    
    
    /**
     * @Description: 方法前处理 
     */
    @Before("weblog()")
    public void doBefore(JoinPoint joinPoint) 
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info("#####################请求开始####################");
        // 记录下请求内容
        logger.info("URL : " + request.getRequestURL().toString());
        logger.info("HTTP_METHOD : " + request.getMethod());
        logger.info("IP : " + request.getRemoteAddr());
        Enumeration<String> enu = request.getParameterNames();
        while (enu.hasMoreElements()) 
            String name = (String) enu.nextElement();
            logger.info("name:" + name + ",value:" + request.getParameter(name) + "");
        
    
    
    /**
     * @Description: 方法后处理 
     */
    @AfterReturning(returning="ret",pointcut="weblog()")
    public void doAfterReturning(Object ret) 
        // 处理完请求,返回内容
        logger.info("RESPONSE : " + ret);
        logger.info("#####################请求结束####################");
    

 

测试验证:http://localhost:8080/insertTest1ByService?name=wangsan&age=10

日志:

2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : #####################请求开始####################
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : URL : http://localhost:8080/insertTest1ByService
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : HTTP_METHOD : GET
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : IP : 0:0:0:0:0:0:0:1
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : name:name,value:wangsan
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : name:age,value:10
2019-07-23 18:24:16.490  INFO 2988 --- [nio-8080-exec-5] com.wjy.controller.UserController        : insertTest1ByService:wangsan,10
2019-07-23 18:24:16.572  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : RESPONSE : success
2019-07-23 18:24:16.572  INFO 2988 --- [nio-8080-exec-5] com.wjy.aop.WebLogAspect                 : #####################请求结束####################

 

以上是关于Spring Boot学习之五日志管理的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习之日志管理(二十一)

spring boot 日志/页面处理实体类构建后台管理

Spring Boot学习之四Spring Boot事务管理

妈妈再也不担心我学习之MySQL数据库必备技能日志管理备份与恢复

Spring Boot 日志管理

Spring Boot日志管理