使用注解 日志打印

package com.dianping.credit.prevention.admin.aop;

/**
 * 日志注解
 * @Author: dainan
 * @Date: 2019/10/17 15:18
 * @Description:
 */
public @interface OperateLog {

}

切面代码

package com.dianping.credit.prevention.admin.aop;

import com.dianping.cat.Cat;
import com.dianping.credit.prevention.admin.utils.JacksonUtils;
import com.meituan.inf.xmdlog.XMDLogFormat;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 各个操作日志记录
 *
 * @Author: dainan
 * @Date: 2019/10/17 14:34
 * @Description:
 */
@Aspect
@Slf4j
@Component
public class OperateLogAspect {
    /**
     * 类名
     */
    private String CLASS_NAME = "className";
    /**
     * 方法
     */
    private String METHOD = "method";
    /**
     * 参数
     */
    private String OPERATE_PARA = "methodInvokeLog";
    /**
     * 结果
     */
    private String RESULT = "result";

    private String MESSAGE = "KZT_LOG 主控台";

    /**
     * 打印日志
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Around("@annotation(com.dianping.credit.prevention.admin.aop.OperateLog))")
    public Object operateLog(ProceedingJoinPoint joinPoint) throws Throwable {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String method = joinPoint.getSignature().getName();
        String methodInvokeLog = buildMethodInvokeLog(joinPoint);
        Object result = null;
        try {
            result = joinPoint.proceed();
            log.info("{}=>{}", methodInvokeLog,result);
            Cat.logEvent(className, method);
        } catch (Throwable e) {
            log.error(String.format("%s => fail", methodInvokeLog), e);
            throw e;
        }
        String xmdLogFormat = XMDLogFormat.build().putTag(CLASS_NAME,className)
                .putTag(METHOD,method)
                .putTag(OPERATE_PARA,methodInvokeLog)
                .putTag(RESULT, JacksonUtils.toString(result)).message(MESSAGE);
        log.info(xmdLogFormat);
        return result;
    }

    /**
     * 获取切点名
     *
     * @param joinPoint
     * @return
     */
    private String buildMethodInvokeLog(ProceedingJoinPoint joinPoint) {
        StringBuilder sb = new StringBuilder();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String method = joinPoint.getSignature().getName();
        sb.append(className + "." + method + "(");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            sb.append(arg);
        }
        sb.append(")");
        return sb.toString();
    }
}
 @Pointcut("execution(* com.sankuai.credit.caselibrary.server.service.impl.*.*(..))")
    private void service() {
    }

    @Around("service()||@annotation(com.sankuai.credit.caselibrary.server.aop.Log)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodInvokeLog = buildMethodInvokeLog(joinPoint);
        try {
            Object result = joinPoint.proceed();
            log.info("[LogAspect]{}=>{}", methodInvokeLog, CommonUtils.toJsonString(result));
            return result;
        } catch (Throwable e) {
            log.error("[LogAspect]{}=>fail", methodInvokeLog, e);
            throw e;
        }
    }

    private String buildMethodInvokeLog(ProceedingJoinPoint joinPoint) {
        StringBuilder sb = new StringBuilder();
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String method = joinPoint.getSignature().getName();
        sb.append(className + "." + method + "(");
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            sb.append(args[i]);
            if (i != args.length - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        return sb.toString();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 @Slf4j 注解和 AOP 统一处理打印日志,具体实现可参考以下代码: 1. 引入依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> ``` 2. 在应用主类上添加 @EnableAspectJAutoProxy 注解启用 AOP: ```java @SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 定义切面类: ```java @Aspect @Component @Slf4j public class LogAspect { @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)" + "||@annotation(org.springframework.web.bind.annotation.GetMapping)" + "||@annotation(org.springframework.web.bind.annotation.PostMapping)" + "||@annotation(org.springframework.web.bind.annotation.PutMapping)" + "||@annotation(org.springframework.web.bind.annotation.DeleteMapping)") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录请求内容 log.info("URL : " + request.getRequestURL().toString()); log.info("HTTP_METHOD : " + request.getMethod()); log.info("IP : " + request.getRemoteAddr()); // 记录调用方法 log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); // 记录请求参数 log.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) { // 记录响应内容 log.info("RESPONSE : " + ret); } } ``` 4. 在需要打印日志的接口方法上添加 @RequestMapping 等注解即可。 压缩文件的操作请参考以下代码: ```java public static void zip(Path sourcePath, Path zipPath) throws IOException { try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipPath)); Stream<Path> paths = Files.walk(sourcePath)) { paths.filter(p -> !Files.isDirectory(p)) .forEach(p -> { ZipEntry entry = new ZipEntry(sourcePath.relativize(p).toString()); try { zos.putNextEntry(entry); zos.write(Files.readAllBytes(p)); zos.closeEntry(); } catch (IOException e) { e.printStackTrace(); } }); } } ``` 对于中文加密的问题,我不是很确定您要表达的意思,请再提供更详细的问题描述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值