自己寫的工具類,方便調試方法運行時間,代碼如下:
package com.example.log;
/**
* User: Jimmy
* Date: 2020/1/19
* Desc:
*/
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
public class TimeUtils {
private static Map<String, Long> timeMap = Maps.newHashMap();
private static DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
//接入日誌組件,當不需要顯示打印消息時,不用手動取消
private static Log log = LogFactory.getLog(TimeUtils.class);
/**
* 開始記錄
* @param logName
*/
public static void startLog(String logName) {
long startTime = System.currentTimeMillis();
timeMap.put(startTimeKey(logName), startTime);
log.info(String.format("[%s] START TIME=>[%s],currentTimeMillis=>%d"
,logName
,dtf.print(new DateTime(startTime))
,startTime));
}
/**
* 開始記錄
*/
public static void startLog() {
String logName = generatorLogName();
startLog(logName);
}
/**
* startTimeKey
* @param logName
* @return
*/
private static String startTimeKey(String logName) {
return Thread.currentThread().getName() + "-" + StringUtils.stripToEmpty(logName);
}
/**
* 開始記錄
*/
public static long endLog() {
String logName = generatorLogName();
return endLog(logName);
}
/**
* 結束日誌
* @param logName
* @return 時間戳
*/
public static long endLog(String logName) {
long endTime = System.currentTimeMillis();
if (!timeMap.containsKey(startTimeKey(logName))) {
return 0;
}
long startTime = timeMap.remove(startTimeKey(logName));
log.info(String.format("[%s] END TIME=>[%s],currentTimeMillis=>%d"
,logName
,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(endTime))
,endTime));
log.info(String.format("[%s] 耗時=>%d ms",logName,endTime - startTime));
return endTime - startTime;
}
private static String generatorLogName() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
StackTraceElement log = stackTrace[1];
String tag = null;
//如果class名稱不爲自己,則爲調用該方法的className.methodName
for (int i = 1; i < stackTrace.length; i++) {
StackTraceElement e = stackTrace[i];
if (!e.getClassName().equals(log.getClassName())) {
tag = e.getClassName() + "." + e.getMethodName();
break;
}
}
if (tag == null) {
tag = log.getClassName() + "." + log.getMethodName();
}
return tag;
}
}
依賴 commons-loggiing,commons-lang3,joda-time,guava,不想依賴可以自己改造。
測試類:
public class Test {
public static void main(String args[]){
TimeUtils.startLog();
TimeUtils.startLog("testTimeUtils");
for(int i = 1;i<10;i++){
TimeUtils.startLog("logName" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
TimeUtils.endLog("logName" + i);
}
TimeUtils.endLog("testTimeUtils");
TimeUtils.endLog();
}
}
打印結果:
17:29:21.302 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] START TIME=>[2020-01-19 17:29:21.184],currentTimeMillis=>1579426161184
17:29:21.308 [main] INFO com.example.log.TimeUtils - [testTimeUtils] START TIME=>[2020-01-19 17:29:21.307],currentTimeMillis=>1579426161307
17:29:21.308 [main] INFO com.example.log.TimeUtils - [logName1] START TIME=>[2020-01-19 17:29:21.308],currentTimeMillis=>1579426161308
17:29:22.309 [main] INFO com.example.log.TimeUtils - [logName1] END TIME=>[2020-01-19 17:29:22.309],currentTimeMillis=>1579426162309
17:29:22.309 [main] INFO com.example.log.TimeUtils - [logName1] 耗時=>1001 ms
17:29:22.310 [main] INFO com.example.log.TimeUtils - [logName2] START TIME=>[2020-01-19 17:29:22.309],currentTimeMillis=>1579426162309
17:29:23.310 [main] INFO com.example.log.TimeUtils - [logName2] END TIME=>[2020-01-19 17:29:23.310],currentTimeMillis=>1579426163310
17:29:23.310 [main] INFO com.example.log.TimeUtils - [logName2] 耗時=>1001 ms
17:29:23.311 [main] INFO com.example.log.TimeUtils - [logName3] START TIME=>[2020-01-19 17:29:23.310],currentTimeMillis=>1579426163310
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName3] END TIME=>[2020-01-19 17:29:24.312],currentTimeMillis=>1579426164312
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName3] 耗時=>1002 ms
17:29:24.312 [main] INFO com.example.log.TimeUtils - [logName4] START TIME=>[2020-01-19 17:29:24.312],currentTimeMillis=>1579426164312
17:29:25.312 [main] INFO com.example.log.TimeUtils - [logName4] END TIME=>[2020-01-19 17:29:25.312],currentTimeMillis=>1579426165312
17:29:25.312 [main] INFO com.example.log.TimeUtils - [logName4] 耗時=>1000 ms
17:29:25.313 [main] INFO com.example.log.TimeUtils - [testTimeUtils] END TIME=>[2020-01-19 17:29:25.312],currentTimeMillis=>1579426165312
17:29:25.313 [main] INFO com.example.log.TimeUtils - [testTimeUtils] 耗時=>4005 ms
17:29:25.313 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] END TIME=>[2020-01-19 17:29:25.313],currentTimeMillis=>1579426165313
17:29:25.313 [main] INFO com.example.log.TimeUtils - [com.example.log.Test.main] 耗時=>4129 ms
一年又過去了,時間真的很快。