diff --git a/src/main/java/com/alipay/sofa/common/code/LogCode2Description.java b/src/main/java/com/alipay/sofa/common/code/LogCode2Description.java index 2b6b50d..eeed109 100644 --- a/src/main/java/com/alipay/sofa/common/code/LogCode2Description.java +++ b/src/main/java/com/alipay/sofa/common/code/LogCode2Description.java @@ -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; @@ -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); @@ -99,11 +100,14 @@ public static void removeCodeSpace(SpaceId spaceId) { LOG_CODE_2_DESCRIPTION_MAP.remove(spaceId); } - private final String logFormat; - private final Map codeMap = new ConcurrentHashMap<>(); + private final String logFormat; + private final String logPrefix; + private final Map 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)) { @@ -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) { @@ -161,4 +184,26 @@ private List 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; + } + } } diff --git a/src/main/java/com/alipay/sofa/common/log/Constants.java b/src/main/java/com/alipay/sofa/common/log/Constants.java index d0a6104..e776aa7 100644 --- a/src/main/java/com/alipay/sofa/common/log/Constants.java +++ b/src/main/java/com/alipay/sofa/common/log/Constants.java @@ -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"; diff --git a/src/test/java/com/alipay/sofa/common/log/code/LogCode2DescriptionTest.java b/src/test/java/com/alipay/sofa/common/log/code/LogCode2DescriptionTest.java index 2f49407..8ad46e9 100644 --- a/src/test/java/com/alipay/sofa/common/log/code/LogCode2DescriptionTest.java +++ b/src/test/java/com/alipay/sofa/common/log/code/LogCode2DescriptionTest.java @@ -40,14 +40,20 @@ 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 @@ -55,8 +61,14 @@ 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")); } @@ -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")); } } diff --git a/src/test/resources/SOFA-TEST/log-codes.properties b/src/test/resources/SOFA-TEST/log-codes.properties index 0396c50..bce9f75 100644 --- a/src/test/resources/SOFA-TEST/log-codes.properties +++ b/src/test/resources/SOFA-TEST/log-codes.properties @@ -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 diff --git a/src/test/resources/SOFA-TEST/log-codes_zh_CN.properties b/src/test/resources/SOFA-TEST/log-codes_zh_CN.properties index 5e767c0..4627c8b 100644 --- a/src/test/resources/SOFA-TEST/log-codes_zh_CN.properties +++ b/src/test/resources/SOFA-TEST/log-codes_zh_CN.properties @@ -1,2 +1,3 @@ +logValueSuffix=, Please see doc http://www.sofaboot.com?errorcode=%s-%s 00-00000=一切都好 01-00001=出现了问题