From f3a6c4984cd52f27a46081daa12e6ae851d16ffa Mon Sep 17 00:00:00 2001 From: King John Date: Fri, 26 Jul 2024 14:23:16 +0800 Subject: [PATCH] [#11290] full logger pattern replace, logback --- .../profiles/release/pinpoint.config | 4 +- .../plugin/logback/LogbackConfig.java | 17 ++++++++- .../interceptor/PatternLayoutInterceptor.java | 37 ++++++++++++++----- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/agent-module/agent/src/main/resources/profiles/release/pinpoint.config b/agent-module/agent/src/main/resources/profiles/release/pinpoint.config index 3ec4d85f5b91..78ba075c1275 100644 --- a/agent-module/agent/src/main/resources/profiles/release/pinpoint.config +++ b/agent-module/agent/src/main/resources/profiles/release/pinpoint.config @@ -1188,7 +1188,9 @@ profiler.logback.logging.transactioninfo=false # variables/aliases: https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/java/ch/qos/logback/classic/PatternLayout.java#L54-L149 #profiler.logback.logging.pattern.replace.enable=false #profiler.logback.logging.pattern.replace.search=%message,%msg,%m -#profiler.logback.logging.pattern.replace.with="TxId:%X{PtxId} %msg" +#profiler.logback.logging.pattern.replace.with=TxId:%X{PtxId} %msg +# the logger pattern would be fully replaced with the configured value if replace.enable=true +#profiler.logback.logging.pattern.full_replace.with= ########################################################### # google httpclient diff --git a/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/LogbackConfig.java b/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/LogbackConfig.java index 30808f51dd1f..169ccef48291 100644 --- a/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/LogbackConfig.java +++ b/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/LogbackConfig.java @@ -31,12 +31,15 @@ public class LogbackConfig { private static final String LOGBACK_LOGGING_PATTERN_REPLACE_ENABLE = "profiler.logback.logging.pattern.replace.enable"; private static final String LOGBACK_LOGGING_PATTERN_REPLACE_SEARCH = "profiler.logback.logging.pattern.replace.search"; private static final String LOGBACK_LOGGING_PATTERN_REPLACE_WITH = "profiler.logback.logging.pattern.replace.with"; + private static final String LOGBACK_LOGGING_PATTERN_FULL_REPLACE_WITH = "profiler.logback.logging.pattern.full_replace.with"; private final boolean logbackLoggingTransactionInfo; private final boolean patternReplaceEnable; + private final boolean patternFullReplace; private final List patternReplaceSearchList; private final String patternReplaceWith; + private final String patternFullReplaceWith; public LogbackConfig(ProfilerConfig config) { @@ -44,9 +47,11 @@ public LogbackConfig(ProfilerConfig config) { this.patternReplaceSearchList = config.readList(LOGBACK_LOGGING_PATTERN_REPLACE_SEARCH); this.patternReplaceWith = config.readString(LOGBACK_LOGGING_PATTERN_REPLACE_WITH, ""); + this.patternFullReplaceWith = config.readString(LOGBACK_LOGGING_PATTERN_FULL_REPLACE_WITH, ""); boolean configEnabled = config.readBoolean(LOGBACK_LOGGING_PATTERN_REPLACE_ENABLE, false); - boolean configOk = !CollectionUtils.isEmpty(patternReplaceSearchList) && StringUtils.hasText(patternReplaceWith); + boolean configOk = (!CollectionUtils.isEmpty(patternReplaceSearchList) && StringUtils.hasText(patternReplaceWith)) || StringUtils.hasText(patternFullReplaceWith); this.patternReplaceEnable = configEnabled && configOk; + this.patternFullReplace = configEnabled && StringUtils.hasText(patternFullReplaceWith); } public boolean isLogbackLoggingTransactionInfo() { @@ -57,6 +62,10 @@ public boolean isPatternReplaceEnable() { return patternReplaceEnable; } + public boolean isPatternFullReplace() { + return patternFullReplace; + } + public List getPatternReplaceSearchList() { return patternReplaceSearchList; } @@ -65,13 +74,19 @@ public String getPatternReplaceWith() { return patternReplaceWith; } + public String getPatternFullReplaceWith() { + return patternFullReplaceWith; + } + @Override public String toString() { return "LogbackConfig{" + "logbackLoggingTransactionInfo=" + logbackLoggingTransactionInfo + ", patternReplaceEnable=" + patternReplaceEnable + + ", patternFullReplace='" + patternFullReplace + '\'' + ", patternReplaceSearchList=" + patternReplaceSearchList + ", patternReplaceWith='" + patternReplaceWith + '\'' + + ", patternFullReplaceWith='" + patternFullReplaceWith + '\'' + '}'; } } diff --git a/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/interceptor/PatternLayoutInterceptor.java b/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/interceptor/PatternLayoutInterceptor.java index 518317719808..7bf12ffab0da 100644 --- a/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/interceptor/PatternLayoutInterceptor.java +++ b/agent-module/plugins/logback/src/main/java/com/navercorp/pinpoint/plugin/logback/interceptor/PatternLayoutInterceptor.java @@ -34,10 +34,10 @@ public class PatternLayoutInterceptor implements AroundInterceptor1 { private final PluginLogger logger = PluginLogManager.getLogger(this.getClass()); private final boolean debug = logger.isDebugEnabled(); - private final LogbackConfig logbackConfig; + private final LogbackConfig config; public PatternLayoutInterceptor(TraceContext traceContext) { - this.logbackConfig = new LogbackConfig(traceContext.getProfilerConfig()); + this.config = new LogbackConfig(traceContext.getProfilerConfig()); } @Override @@ -50,35 +50,52 @@ public void after(Object target, Object arg0, Object result, Throwable throwable return; } String oldPattern = (String) arg0; + if (config.isPatternFullReplace()) { + updatePattern(target, oldPattern, config.getPatternFullReplaceWith()); + return; + } if (oldPattern.contains(PATTERN_TRANSACTION_ID)) { if (debug) { logger.debug("Logback pattern already have pinpoint pattern, pattern:{}", oldPattern); } return; } - updatePattern(target, oldPattern, logbackConfig.getPatternReplaceSearchList(), logbackConfig.getPatternReplaceWith()); + updatePattern(target, oldPattern, config.getPatternReplaceSearchList(), config.getPatternReplaceWith()); } - private void updatePattern(Object target, String oldPattern, List searchList, String replace) { - if (!(target instanceof PatternLayoutBase)) { + private void updatePattern(Object target, String old, String replace) { + if (replace.contentEquals(old)) { return; } + if (updatePattern(target, replace)) { + logger.info("Logback pattern fully-replaced, old pattern({}) and new pattern({})", old, replace); + } + } + + private void updatePattern(Object target, String oldPattern, List searchList, String replace) { String newPattern = oldPattern; boolean changed = false; for (String search : searchList) { newPattern = oldPattern.replace(search, replace); if (!oldPattern.contentEquals(newPattern)) { changed = true; - if (debug) { - logger.debug("Logback pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern); - } break; } } if (changed) { - final PatternLayoutBase patternLayout = (PatternLayoutBase) target; - patternLayout.setPattern(newPattern); + if (updatePattern(target, newPattern)) { + logger.info("Logback pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern); + } + } + } + + private boolean updatePattern(Object target, String pattern) { + if (!(target instanceof PatternLayoutBase)) { + return false; } + final PatternLayoutBase patternLayout = (PatternLayoutBase) target; + patternLayout.setPattern(pattern); + return true; } }