项目中日志输出例子

package com.qlead.pams;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Aspect
@Component
public class PamsRequestLogger {
    @Pointcut("execution(public * com.qlead.pams.controller.*Controller.*(..))")
    public void log() {
    }

    @Before("log()")
    public void beforeLog(JoinPoint point) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        log.debug("[REQUEST USER] {} :{}", request.getRemoteAddr(), request.getHeader("User-Agent"));
        log.debug("[REQUEST TARGET] {} :{}:{}()", request.getRequestURL(), point.getSignature().getDeclaringTypeName(), point.getSignature().getName());

        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap != null) {
            log.debug("[REQUEST PARAM]: {}", JSONObject.toJSONString(parameterMap));
        }

        request.setAttribute("START_TIME", System.currentTimeMillis());
    }

    @AfterReturning("log()")
    public void afterReturning() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        long start = (Long) request.getAttribute("START_TIME");
        log.debug("[DURATION] {}ms", System.currentTimeMillis() - start);
    }
}

部署到服务器中,可以在控制台中看到日志:

2020-02-27 15:46:50.149 DEBUG 16910 --- [nio-8088-exec-8] com.qlead.pams.PamsRequestLogger         : [REQUEST USER] 172.205.9.124 :Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
2020-02-27 15:46:50.150 DEBUG 16910 --- [nio-8088-exec-8] com.qlead.pams.PamsRequestLogger         : [REQUEST TARGET] http://172.205.9.124:8088/unit/all :com.qlead.pams.controller.UnitController.retrieveAll.()
2020-02-27 15:46:50.150 DEBUG 16910 --- [nio-8088-exec-8] com.qlead.pams.PamsRequestLogger         : [REQUEST PARAM]: no param
2020-02-27 15:46:50.151 DEBUG 16910 --- [nio-8088-exec-8] c.q.pams.mapper.UnitMapper.selectList    : ==>  Preparing: SELECT u_id AS id,u_account_id AS accountId,u_name AS name,u_manager AS manager,u_cellphone AS cellphone,u_type AS type,u_status AS status,u_create_time AS createTime,u_modified_time AS modifiedTime FROM tb_unit 
2020-02-27 15:46:50.151 DEBUG 16910 --- [nio-8088-exec-8] c.q.pams.mapper.UnitMapper.selectList    : ==> Parameters: 
2020-02-27 15:46:50.154 DEBUG 16910 --- [nio-8088-exec-8] c.q.pams.mapper.UnitMapper.selectList    : <==      Total: 32
2020-02-27 15:46:50.154 DEBUG 16910 --- [nio-8088-exec-8] com.qlead.pams.PamsRequestLogger         : [DURATION] 4ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值