From 8d4ed1ad06b64381ba7d8495c9a46268f49e3a48 Mon Sep 17 00:00:00 2001 From: HzjNeverStop <441627022@qq.com> Date: Mon, 14 Aug 2023 10:50:38 +0800 Subject: [PATCH] 1. update 1.3.12 (#187) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1. update 1.3.12 2. update ci config 3. support error code property priority * error code support docUrl * error code support docUrl --------- Co-authored-by: “HzjNeverStop” <“441627022@qq.com”> --- .github/workflows/ci.yml | 6 +- pom.xml | 2 +- .../sofa/common/code/LogCode2Description.java | 61 ++++++++++++++++--- .../com/alipay/sofa/common/log/Constants.java | 1 + .../log/code/LogCode2DescriptionTest.java | 40 ++++++++---- .../resources/SOFA-TEST/log-codes.properties | 1 + .../SOFA-TEST/log-codes_zh_CN.properties | 1 + 7 files changed, 89 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a561f7c..c5ca19cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,11 @@ name: build on: push: - branches: [ master ] + branches: + - '**' pull_request: - branches: [ master ] + branches: + - '**' jobs: build: diff --git a/pom.xml b/pom.xml index 9cd3513e..51235b5a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.alipay.sofa.common sofa-common-tools - 1.3.11 + 1.3.12 jar ${project.groupId}:${project.artifactId} 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 e70434d3..a6a30794 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 String logFormat; - private Map codeMap = new ConcurrentHashMap<>(); + private String logFormat; + private String logPrefix; + private String logValueSuffix; + private Map codeMap = new ConcurrentHashMap<>(); 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 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 d0a6104f..e776aa7d 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 2f494074..8ad46e9a 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 0396c50e..bce9f754 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 5e767c09..4627c8bc 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=出现了问题