1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.yanzuoguang.util.log;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.Date;
/**
* 日志追踪器
*
* @author Light
*/
public class LogDate {
public static final int MIN_MILL_SECOND = 1;
private StringBuilder log = new StringBuilder();
private long start = System.currentTimeMillis();
private long end = System.currentTimeMillis();
private double lastSecond = 0;
private double totalSecond = 0;
/**
* 构造函数
*/
public LogDate() {
this("");
}
/**
* 构造函数
*
* @param title 标记
*/
public LogDate(String title) {
this.clear();
if (!StringHelper.isEmpty(title)) {
this.begin(title);
}
}
/**
* 开始记录
*/
public void begin() {
this.start = System.currentTimeMillis();
}
/**
* 开始标记
*
* @param title 开始标记
*/
public void begin(String title) {
this.begin();
this.log.append(title);
}
/**
* 提交日志,用于跟踪时间
*/
public void commit() {
this.end = System.currentTimeMillis();
double total = this.end - this.start;
this.lastSecond = total - this.totalSecond;
this.totalSecond = total;
}
/**
* 提交日志记录用于跟踪
*
* @param tag 标记
* @param args 参数
*/
public void commit(String tag, Object... args) {
this.commit();
if (!StringHelper.isEmpty(tag)) {
String log = String.format("%s: %f ms 总共: %f ms ", String.format(tag, args), this.lastSecond, this.totalSecond);
this.log.append(log);
}
}
/**
* 将当前日志对象复位
*/
public void clear() {
this.start = System.currentTimeMillis();
this.end = System.currentTimeMillis();
this.lastSecond = 0;
this.totalSecond = 0;
this.log = new StringBuilder();
}
/**
* 将当前日志对象写入到日志中
*/
public void write() {
// 执行时间为0的不显示日志
if (this.totalSecond >= MIN_MILL_SECOND) {
String vLog = this.log.toString();
if (!StringHelper.isEmpty(vLog)) {
Log.info(LogDate.class, this.log.toString());
}
this.clear();
}
}
/**
* 获取日志内容
*
* @return
*/
public StringBuilder getLog() {
return this.log;
}
/**
* 获取开始时间
*
* @return
*/
public Date getStart() {
return new Date(this.start) ;
}
/**
* 获取结束时间
*
* @return
*/
public Date getEnd() {
return new Date(this.end);
}
/**
* 获取最后处理时间
*
* @return
*/
public double getLastSecond() {
return this.lastSecond;
}
/**
* 获取总执行时间
*
* @return
*/
public double getTotalSecond() {
return this.totalSecond;
}
}