Skip to content

Commit d5c01d7

Browse files
committed
upgrade txlcn logger.
1 parent 9e43bb7 commit d5c01d7

16 files changed

Lines changed: 325 additions & 59 deletions

File tree

txlcn-common/src/main/java/com/codingapi/txlcn/common/util/Maps.java

Lines changed: 213 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616
package com.codingapi.txlcn.common.util;
1717

18-
import java.util.HashMap;
19-
import java.util.Map;
18+
import java.util.*;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
2021

2122
/**
2223
* Description:
@@ -50,4 +51,214 @@ public static <K, V> Map<K, V> newHashMap(K key1, V value1, K key2, V value2, K
5051
map.put(key4, value4);
5152
return map;
5253
}
54+
55+
public static <K, V> Map<K, V> newImmutableMap(K key1, V value1) {
56+
ImmutableMap<K, V> map = new ImmutableMap<>();
57+
map.put(key1, value1);
58+
return map;
59+
}
60+
61+
public static <K, V> Map<K, V> newImmutableMap(K key1, V value1, K key2, V value2) {
62+
ImmutableMap<K, V> map = new ImmutableMap<>();
63+
map.put(key1, value1);
64+
map.put(key2, value2);
65+
return map;
66+
}
67+
68+
public static <K, V> Map<K, V> newImmutableMap(K key1, V value1, K key2, V value2, K key3, V value3) {
69+
Map<K, V> map = newImmutableMap(key1, value1, key2, value2);
70+
map.put(key3, value3);
71+
return map;
72+
}
73+
74+
public static <K, V> Map<K, V> newImmutableMap(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4) {
75+
Map<K, V> map = newImmutableMap(key1, value1, key2, value2, key3, value3);
76+
map.put(key3, value4);
77+
return map;
78+
}
79+
80+
static class ImmutableMap<K, V> implements Map<K, V> {
81+
82+
private K key1;
83+
84+
private V value1;
85+
86+
private K key2;
87+
88+
private V value2;
89+
90+
private K key3;
91+
92+
private V value3;
93+
94+
private K key4;
95+
96+
private V value4;
97+
98+
private int size;
99+
100+
private int maxSize = 4;
101+
102+
ImmutableMap() {
103+
104+
}
105+
106+
@Override
107+
public int size() {
108+
return size;
109+
}
110+
111+
@Override
112+
public boolean isEmpty() {
113+
return size == 0;
114+
}
115+
116+
@Override
117+
public boolean containsKey(Object o) {
118+
return Objects.nonNull(key1) && key1.equals(o) ||
119+
Objects.nonNull(key2) && key2.equals(o) ||
120+
Objects.nonNull(key3) && key3.equals(o) ||
121+
Objects.nonNull(key4) && key4.equals(o);
122+
}
123+
124+
@Override
125+
public boolean containsValue(Object o) {
126+
return Objects.nonNull(value1) && value1.equals(o) ||
127+
Objects.nonNull(value2) && value2.equals(o) ||
128+
Objects.nonNull(value3) && value3.equals(o) ||
129+
Objects.nonNull(value4) && value4.equals(o);
130+
}
131+
132+
@Override
133+
public V get(Object o) {
134+
if (!containsKey(o)) {
135+
return null;
136+
}
137+
return Objects.nonNull(key1) && key1.equals(o) ? value1 :
138+
(Objects.nonNull(key2) && key2.equals(o) ? value2 :
139+
(Objects.nonNull(key3) && key3.equals(o) ? value3 :
140+
(Objects.nonNull(key4) && key4.equals(o) ? value4 : null)));
141+
}
142+
143+
@Override
144+
public V put(K k, V v) {
145+
if (Objects.isNull(key1)) {
146+
key1 = k;
147+
value1 = v;
148+
size++;
149+
return v;
150+
} else if (Objects.isNull(key2)) {
151+
key2 = k;
152+
value2 = v;
153+
size++;
154+
return v;
155+
} else if (Objects.isNull(key3)) {
156+
key3 = k;
157+
value3 = v;
158+
size++;
159+
return v;
160+
} else if (Objects.isNull(key4)) {
161+
key4 = k;
162+
value4 = v;
163+
size++;
164+
return v;
165+
}
166+
int index = keyIndex(k);
167+
if (index != -1) {
168+
V oldV = getValueByKeyIndex(index);
169+
setValueByKeyIndex(index, v);
170+
return oldV;
171+
}
172+
throw new IllegalStateException("ImmutableMap is full.");
173+
}
174+
175+
@Override
176+
public V remove(Object o) {
177+
if (containsKey(o)) {
178+
int index = keyIndex((K) o);
179+
V oldV = getValueByKeyIndex(index);
180+
setValueByKeyIndex(index, null);
181+
size--;
182+
return oldV;
183+
}
184+
return null;
185+
}
186+
187+
@Override
188+
public void putAll(Map<? extends K, ? extends V> map) {
189+
Objects.requireNonNull(map);
190+
int i = 0;
191+
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
192+
if (++i > maxSize) {
193+
break;
194+
}
195+
put(entry.getKey(), entry.getValue());
196+
}
197+
198+
}
199+
200+
@Override
201+
public void clear() {
202+
size = 0;
203+
key1 = null;
204+
value1 = null;
205+
key2 = null;
206+
value2 = null;
207+
key3 = null;
208+
value3 = null;
209+
key4 = null;
210+
value4 = null;
211+
}
212+
213+
@Override
214+
public Set<K> keySet() {
215+
return Stream.of(key1, key2, key3, key4)
216+
.filter(Objects::nonNull)
217+
.collect(Collectors.toSet());
218+
}
219+
220+
@Override
221+
public Collection<V> values() {
222+
return Stream.of(value1, value2, value3, value4)
223+
.filter(Objects::nonNull)
224+
.collect(Collectors.toSet());
225+
}
226+
227+
@Override
228+
public Set<Entry<K, V>> entrySet() {
229+
throw new UnsupportedOperationException("not support entey set.");
230+
}
231+
232+
private int keyIndex(K o) {
233+
return Objects.nonNull(key1) && key1.equals(o) ? 1 :
234+
(Objects.nonNull(key2) && key2.equals(o) ? 2 :
235+
(Objects.nonNull(key3) && key3.equals(o) ? 3 :
236+
(Objects.nonNull(key4) && key4.equals(o) ? 4 : -1)));
237+
}
238+
239+
private void setValueByKeyIndex(int index, V value) {
240+
if (index == 1) {
241+
this.value1 = value;
242+
} else if (index == 2) {
243+
this.value2 = value;
244+
} else if (index == 3) {
245+
this.value3 = value;
246+
} else if (index == 4) {
247+
this.value4 = value;
248+
}
249+
}
250+
251+
private V getValueByKeyIndex(int index) {
252+
if (index == 1) {
253+
return this.value1;
254+
} else if (index == 2) {
255+
return this.value2;
256+
} else if (index == 3) {
257+
return this.value3;
258+
} else if (index == 4) {
259+
return this.value4;
260+
}
261+
return null;
262+
}
263+
}
53264
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.codingapi.txlcn.common.util;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
7+
/**
8+
* Description:
9+
* Date: 2/1/19
10+
*
11+
* @author ujued
12+
*/
13+
public abstract class Strings {
14+
15+
public static String format(String input, Map<String, Object> params, Object... args) {
16+
StringBuilder varString = new StringBuilder();
17+
StringBuilder finalString = new StringBuilder();
18+
int varMaxLen = 5;
19+
int curVarLen = 0;
20+
boolean wait = false;
21+
int argIndex = -1;
22+
char startChar = '%';
23+
for (int i = 0; i < input.length(); i++) {
24+
char c = input.charAt(i);
25+
if (c == '{' || c == '%') {
26+
wait = true;
27+
startChar = c;
28+
continue;
29+
}
30+
if (wait) {
31+
if (c == '}' || (startChar == '%' && (c == 's' || c == 'd'))) {
32+
if (varString.length() > 0) {
33+
finalString.append(Optional.ofNullable(params.get(varString.toString())).orElse(startChar + c));
34+
curVarLen = 0;
35+
varString.delete(0, varString.length());
36+
} else if (++argIndex >= args.length) {
37+
finalString.append(startChar).append(c);
38+
} else {
39+
finalString.append(args[argIndex]);
40+
}
41+
} else if (curVarLen < varMaxLen) {
42+
varString.append(c);
43+
curVarLen++;
44+
continue;
45+
} else {
46+
finalString.append(startChar).append(varString);
47+
curVarLen = 0;
48+
varString.delete(0, varString.length());
49+
}
50+
wait = false;
51+
continue;
52+
}
53+
finalString.append(c);
54+
}
55+
return finalString.toString();
56+
}
57+
58+
public static void main(String[] args) {
59+
String s1 = "hello, {}. {} is in {}";
60+
System.out.println(format(s1, Maps.newImmutableMap("who", "ujued"), "world", "jinan"));
61+
}
62+
}

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/AbstractTxLogger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import com.codingapi.txlcn.common.util.Transactions;
1919
import com.codingapi.txlcn.logger.db.TxLog;
2020
import lombok.extern.slf4j.Slf4j;
21+
import org.springframework.util.StringUtils;
2122

2223
import java.text.SimpleDateFormat;
2324
import java.util.Date;
24-
import java.util.Objects;
2525
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.Executors;
2727

@@ -45,12 +45,12 @@ public void trace(String groupId, String unitId, String tag, String content, Obj
4545
TxLog txLog = new TxLog();
4646
txLog.setContent(content);
4747
txLog.setArgs(args);
48-
txLog.setGroupId(groupId);
4948
txLog.setTag(tag);
50-
txLog.setUnitId(Objects.isNull(unitId) ? "" : unitId);
49+
txLog.setGroupId(StringUtils.isEmpty(groupId) ? "" : groupId);
50+
txLog.setUnitId(StringUtils.isEmpty(unitId) ? "" : unitId);
5151
txLog.setAppName(Transactions.APPLICATION_ID_WHEN_RUNNING);
5252
txLog.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date()));
53-
log.debug("trace tx-logger -> {}", txLog);
53+
log.debug("{}: " + content, tag, args);
5454
this.loggerSaveService.execute(() -> saveTrace(txLog));
5555
}
5656

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/Slf4jTxLogger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Slf4jTxLogger implements TxLogger {
2828

2929
@Override
3030
public void trace(String groupId, String unitId, String tag, String content, Object... args) {
31-
log.debug("{}: " + content, tag, content, args);
31+
log.debug("{}: " + content, tag, args);
3232
}
3333

3434
@Override
@@ -37,7 +37,7 @@ public void info(String groupId, String unitId, String tag, String content, Obje
3737
}
3838

3939
@Override
40-
public void error(String groupId, String unitId, String content, Object... args) {
41-
log.error("{}: " + content, groupId, unitId, Transactions.TE, args);
40+
public void error(String groupId, String unitId, String tag, String content, Object... args) {
41+
log.error("{}: " + content, tag, args);
4242
}
4343
}

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/TxLogger.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ default void transactionInfo(String groupId, String unitId, String content, Obje
4848
info(groupId, unitId, Transactions.TAG_TRANSACTION, content, args);
4949
}
5050

51+
default void taskInfo(String groupId, String unitId, String content, Object... args) {
52+
info(groupId, unitId, Transactions.TAG_TASK, content, args);
53+
}
54+
5155
/**
5256
* info log. todo
5357
*
@@ -60,17 +64,6 @@ default void info(String groupId, String unitId, String tag, String content, Obj
6064
trace(groupId, unitId, tag, content, args);
6165
}
6266

63-
/**
64-
* error log. todo
65-
*
66-
* @param groupId groupId
67-
* @param unitId unitId
68-
* @param content content
69-
*/
70-
default void error(String groupId, String unitId, String content, Object... args) {
71-
trace(groupId, unitId, Transactions.TE, content, args);
72-
}
73-
7467
default void error(String groupId, String unitId, String tag, String content, Object... args) {
7568
trace(groupId, unitId, tag, content, args);
7669
}

txlcn-logger/src/main/java/com/codingapi/txlcn/logger/helper/MysqlLoggerHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.codingapi.txlcn.logger.helper;
1717

18+
import com.codingapi.txlcn.common.util.Maps;
19+
import com.codingapi.txlcn.common.util.Strings;
1820
import com.codingapi.txlcn.logger.db.LogDbHelper;
1921
import com.codingapi.txlcn.logger.db.LogDbProperties;
2022
import com.codingapi.txlcn.logger.db.TxLog;
@@ -75,7 +77,10 @@ public int insert(TxLog txLoggerInfo) {
7577
if (logDbProperties.isEnabled()) {
7678
String sql = "insert into t_logger(group_id,unit_id,tag,content,create_time,app_name) values(?,?,?,?,?,?)";
7779
return dbHelper.update(sql, txLoggerInfo.getGroupId(), txLoggerInfo.getUnitId(), txLoggerInfo.getTag(),
78-
String.format(txLoggerInfo.getContent(), txLoggerInfo.getArgs()), txLoggerInfo.getCreateTime(), txLoggerInfo.getAppName());
80+
Strings.format(
81+
txLoggerInfo.getContent(), Maps.newImmutableMap(
82+
"xid", txLoggerInfo.getGroupId(), "uid", txLoggerInfo.getUnitId()),
83+
txLoggerInfo.getArgs()), txLoggerInfo.getCreateTime(), txLoggerInfo.getAppName());
7984
} else {
8085
throw new NotEnableLogException("not enable logger");
8186
}

txlcn-tc/src/main/java/com/codingapi/txlcn/tc/core/DTXServiceExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Object transactionRunning(TxTransactionInfo info) throws Throwable {
8686

8787
// 4.2 业务执行前
8888
txLogger.transactionInfo(
89-
info.getGroupId(), info.getUnitId(), "pre business code, unit type: %s", transactionType);
89+
info.getGroupId(), info.getUnitId(), "pre business code, unit type: {}", transactionType);
9090

9191
// 4.3 执行业务
9292
Object result = dtxLocalControl.doBusinessCode(info);

0 commit comments

Comments
 (0)