Skip to content

Commit

Permalink
1.error code support property priority (#189)
Browse files Browse the repository at this point in the history
2.error code support docUrl
  • Loading branch information
HzjNeverStop committed Aug 16, 2023
1 parent ec43d9b commit e0ff800
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
61 changes: 53 additions & 8 deletions src/main/java/com/alipay/sofa/common/code/LogCode2Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.sofa.common.code;

import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.space.SpaceId;
import com.alipay.sofa.common.utils.ReportUtil;
import com.alipay.sofa.common.utils.StringUtil;
Expand Down Expand Up @@ -61,14 +62,14 @@ public static LogCode2Description create(String spaceName) {
public static LogCode2Description create(SpaceId spaceId) {
if (isCodeSpaceInitialized(spaceId)) {
ReportUtil.reportWarn("Code space: \"" + spaceId.getSpaceName()
+ "\" is already initialized!");
+ "\" is already initialized!");
return LOG_CODE_2_DESCRIPTION_MAP.get(spaceId);
}

synchronized (spaceId) {
if (isCodeSpaceInitialized(spaceId)) {
ReportUtil.reportWarn("Code space: \"" + spaceId.getSpaceName()
+ "\" is already initialized!");
+ "\" is already initialized!");
return LOG_CODE_2_DESCRIPTION_MAP.get(spaceId);
}
LogCode2Description logCode2Description = doCreate(spaceId);
Expand Down Expand Up @@ -99,11 +100,14 @@ public static void removeCodeSpace(SpaceId spaceId) {
LOG_CODE_2_DESCRIPTION_MAP.remove(spaceId);
}

private final String logFormat;
private final Map<String, String> codeMap = new ConcurrentHashMap<>();
private final String logFormat;
private final String logPrefix;
private final Map<String, Pair> codeMap = new ConcurrentHashMap<>();
private String logValueSuffix;

private LogCode2Description(SpaceId spaceId) {
logFormat = spaceId.getSpaceName().toUpperCase() + "-%s: %s";
logPrefix = spaceId.getSpaceName().toUpperCase();
logFormat = logPrefix + "-%s: %s";
String prefix = spaceId.getSpaceName().replace(".", "/") + "/log-codes";
String encoding = Locale.getDefault().toString();
if (StringUtil.isEmpty(encoding)) {
Expand All @@ -126,19 +130,38 @@ private LogCode2Description(SpaceId spaceId) {
InputStreamReader reader = new InputStreamReader(in);
properties.load(reader);
}
for (Map.Entry<?, ?> entry: properties.entrySet()) {

int priority = Constants.DEFAULT_PRIORITY;
String priorityString = properties.getProperty(Constants.PRIORITY_KEY);
if (StringUtil.isNotEmpty(priorityString)) {
priority = Integer.parseInt(priorityString);
}


for (Map.Entry<?, ?> entry : properties.entrySet()) {
String key = (String) entry.getKey();
codeMap.put(key, String.format(logFormat, key, entry.getValue()));
Pair exist = codeMap.get(key);
// high priority value exist, skip
if (exist != null && exist.getPriority() > priority) {
continue;
}
if (key.equals(Constants.LOG_VALUE_SUFFIX_KEY)) {
logValueSuffix = (String) entry.getValue();
}
codeMap.put(key, new Pair(priority, String.format(logFormat, key, entry.getValue())));
}
} catch (Throwable e) {
ReportUtil.reportError(String.format("Code space \"%s\" initializing failed!", spaceId.getSpaceName()), e);
}
}
if (StringUtil.isNotEmpty(logValueSuffix)) {
codeMap.forEach((key, value) -> value.setValue(value.getValue() + String.format(logValueSuffix, logPrefix, key)));
}
}
}

public String convert(String code) {
return codeMap.computeIfAbsent(code, k -> String.format(logFormat, k, "Unknown Code"));
return codeMap.computeIfAbsent(code, k -> new Pair(0, String.format(logFormat, k, "Unknown Code"))).getValue();
}

public String convert(String code, Object... args) {
Expand All @@ -161,4 +184,26 @@ private List<URL> getResources(ClassLoader classLoader, String path) {
}
return rtn;
}

private static class Pair {
private final Integer priority;
private String value;

public Pair(Integer priority, String value) {
this.priority = priority;
this.value = value;
}

public Integer getPriority() {
return priority;
}

public void setValue(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/alipay/sofa/common/log/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public interface Constants {
String LOG_XML_CONFIG_FILE_ENV_PATTERN = "log-conf-%s.xml";
String LOG_CONFIG_PROPERTIES = "config.properties";
String PRIORITY_KEY = "priority";
String LOG_VALUE_SUFFIX_KEY = "logValueSuffix";
String LOGGER_CONSOLE_WHITE_SET_KEY = "console";
String LOGGER_CONSOLE_PREFIX_WHITE_SET_KEY = "console.prefix";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,35 @@ public void testDefaultLocale() {
Locale.setDefault(new Locale("Unknown"));

LogCode2Description logCode2Description = LogCode2Description.create(COMPONENT_NAME);
Assert.assertEquals("SOFA-TEST-00-00000: All is well",
logCode2Description.convert("00-00000"));
Assert.assertEquals("SOFA-TEST-01-00001: Some things goes wrong",
logCode2Description.convert("01-00001"));
Assert
.assertEquals(
"SOFA-TEST-00-00000: All is well, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-00-00000",
logCode2Description.convert("00-00000"));
Assert
.assertEquals(
"SOFA-TEST-01-00001: Some things goes wrong, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-01-00001",
logCode2Description.convert("01-00001"));
Assert.assertEquals("SOFA-TEST-11-11111: Unknown Code",
logCode2Description.convert("11-11111"));
Assert.assertEquals("SOFA-TEST-02-00000: ABC with argument 123",
logCode2Description.convert("02-00000", "ABC", 123));
Assert
.assertEquals(
"SOFA-TEST-02-00000: ABC with argument 123, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-02-00000",
logCode2Description.convert("02-00000", "ABC", 123));
}

@Test
public void testCnLocale() {
Locale.setDefault(new Locale("zh", "CN"));

LogCode2Description logCode2Description = LogCode2Description.create(COMPONENT_NAME);
Assert.assertEquals("SOFA-TEST-00-00000: 一切都好", logCode2Description.convert("00-00000"));
Assert.assertEquals("SOFA-TEST-01-00001: 出现了问题", logCode2Description.convert("01-00001"));
Assert
.assertEquals(
"SOFA-TEST-00-00000: 一切都好, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-00-00000",
logCode2Description.convert("00-00000"));
Assert
.assertEquals(
"SOFA-TEST-01-00001: 出现了问题, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-01-00001",
logCode2Description.convert("01-00001"));
Assert.assertEquals("SOFA-TEST-11-11111: Unknown Code",
logCode2Description.convert("11-11111"));
}
Expand All @@ -72,9 +84,13 @@ public void alreadyInitialized() {
public void testDirectConvert() {
Locale.setDefault(new Locale("zh", "CN"));

Assert.assertEquals("SOFA-TEST-00-00000: 一切都好",
LogCode2Description.convert(COMPONENT_NAME, "00-00000"));
Assert.assertEquals("SOFA-TEST-01-00001: 出现了问题",
LogCode2Description.convert(COMPONENT_NAME, "01-00001"));
Assert
.assertEquals(
"SOFA-TEST-00-00000: 一切都好, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-00-00000",
LogCode2Description.convert(COMPONENT_NAME, "00-00000"));
Assert
.assertEquals(
"SOFA-TEST-01-00001: 出现了问题, Please see doc http://www.sofaboot.com?errorcode=SOFA-TEST-01-00001",
LogCode2Description.convert(COMPONENT_NAME, "01-00001"));
}
}
1 change: 1 addition & 0 deletions src/test/resources/SOFA-TEST/log-codes.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
logValueSuffix=, Please see doc http://www.sofaboot.com?errorcode=%s-%s
00-00000=All is well
01-00001=Some things goes wrong
02-00000=%s with argument %s
1 change: 1 addition & 0 deletions src/test/resources/SOFA-TEST/log-codes_zh_CN.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
logValueSuffix=, Please see doc http://www.sofaboot.com?errorcode=%s-%s
00-00000=一切都好
01-00001=出现了问题

0 comments on commit e0ff800

Please sign in to comment.