打印代码块执行时间方案,优雅打印执行耗时日志

目的:打印代码块的执行时间

做法:定义工具类 实现 AutoCloseable,重写close方法。初始化时记录开始时间,代码块结束时执行close方法计算时长并打印日志

实现:通过字符串占位符,实现其他日志传入。并且通过重载构建函数,支持传入自定义日志消息,可以将时间打印在日志前面和后面

完整代码如下:(使用方式看 main 方法)

package com.example.demo.utils;

import lombok.extern.slf4j.Slf4j;

/**
 * 统计代码块执行时间
 * 使用方式看 main 函数
 *
 * @Author XZ.Tan
 * @Date: 2021/1/15 10:44
 * @Version 1.0
 */
@Slf4j
public class CountRunTimeCostUtil implements AutoCloseable {

    /**
     * 开始执行时间
     */
    private long startTime;

    /**
     * 日志
     */
    private String msg;

    /**
     * 时间是否打印在日志开始
     */
    private boolean isStart;

    /**
     * 默认耗时加在日志末尾
     *
     * @param msg
     */
    public CountRunTimeCostUtil(String msg, Object... args) {
        String parse = parse(msg, args);
        this.startTime = System.currentTimeMillis();
        this.msg = parse;
        this.isStart = false;
    }

    /**
     * 
     *
     * @param msg
     * @param isStart
     */
    public CountRunTimeCostUtil(Boolean isStart, String msg, Object... args) {
        String parse = parse(msg, args);
        this.startTime = System.currentTimeMillis();
        this.msg = parse;
        if (isStart) {
            this.isStart = true;
        } else {
            this.isStart = false;
        }
    }

    @Override
    public void close() {
        if (isStart) {
            log.info("执行耗时:{}ms, {}", System.currentTimeMillis() - startTime, msg);
        } else {
            log.info("{}, 执行耗时:{}ms", msg, System.currentTimeMillis() - startTime);
        }
    }

    /**
     * 将字符串text中由openToken和closeToken组成的占位符依次替换为args数组中的值
     *
     * @param openToken
     * @param closeToken
     * @param text
     * @param args
     * @return
     */
    public static String parse(String openToken, String closeToken, String text, Object... args) {
        if (args == null || args.length <= 0) {
            return text;
        }
        int argsIndex = 0;

        if (text == null || text.isEmpty()) {
            return "";
        }
        char[] src = text.toCharArray();
        int offset = 0;
        // search open token
        int start = text.indexOf(openToken, offset);
        if (start == -1) {
            return text;
        }
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;
        while (start > -1) {
            if (start > 0 && src[start - 1] == '\\') {
                // this open token is escaped. remove the backslash and continue.
                builder.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                // found open token. let's search close token.
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        // this close token is escaped. remove the backslash and continue.
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        offset = end + closeToken.length();
                        break;
                    }
                }
                if (end == -1) {
                    // close token was not found.
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    ///仅仅修改了该else分支下的个别行代码

                    String value = (argsIndex <= args.length - 1) ?
                            (args[argsIndex] == null ? "" : args[argsIndex].toString()) : expression.toString();
                    builder.append(value);
                    offset = end + closeToken.length();
                    argsIndex++;
                   
                }
            }
            start = text.indexOf(openToken, offset);
        }
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }

    /**
     * 占位符替换
     *
     * @param text
     * @param args
     * @return
     */
    public static String parse(String text, Object... args) {
        return parse("{", "}", text, args);
    }

    /**
     * 如何使用
     *
     * @param args
     */
    public static void main(String[] args) {

        // 耗时
//        try (CountRunTimeCostUtil c = new CountRunTimeCostUtil("{} aabb", "耗时测试")) {
        try (CountRunTimeCostUtil c = new CountRunTimeCostUtil("耗时测试")) {
//        try (CountRunTimeCostUtil c = new CountRunTimeCostUtil(true,"{} aabb", "耗时测试")) {
//        try (CountRunTimeCostUtil c = new CountRunTimeCostUtil(false,"耗时测试")) {
            // 业务代码块 start
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 业务代码块 start
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cy谭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值