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 bd89b032110b..3ec4d85f5b91 100644 --- a/agent-module/agent/src/main/resources/profiles/release/pinpoint.config +++ b/agent-module/agent/src/main/resources/profiles/release/pinpoint.config @@ -1173,7 +1173,9 @@ profiler.log4j2.logging.transactioninfo=false # variables/aliases: https://logging.apache.org/log4j/2.x/manual/layouts.html under section "Patterns" #profiler.log4j2.logging.pattern.replace.enable=false #profiler.log4j2.logging.pattern.replace.search=%message,%msg,%m -#profiler.log4j2.logging.pattern.replace.with="TxId:%X{PtxId} %msg" +#profiler.log4j2.logging.pattern.replace.with=TxId:%X{PtxId} %msg +# the logger pattern would be fully replaced with the configured value if replace.enable=true +#profiler.log4j2.logging.pattern.full_replace.with= ########################################################### # logback (guide url : https://github.com/naver/pinpoint/blob/master/doc/per-request_feature_guide.md) diff --git a/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/Log4j2Config.java b/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/Log4j2Config.java index 7c4c4fde3de7..5e7feb1c4d2c 100644 --- a/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/Log4j2Config.java +++ b/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/Log4j2Config.java @@ -32,12 +32,15 @@ public class Log4j2Config { private static final String LOGGING_PATTERN_REPLACE_ENABLE = "profiler.log4j2.logging.pattern.replace.enable"; private static final String LOGGING_PATTERN_REPLACE_SEARCH = "profiler.log4j2.logging.pattern.replace.search"; private static final String LOGGING_PATTERN_REPLACE_WITH = "profiler.log4j2.logging.pattern.replace.with"; + private static final String LOGGING_PATTERN_FULL_REPLACE_WITH = "profiler.log4j2.logging.pattern.full_replace.with"; private final boolean log4j2LoggingTransactionInfo; private final boolean patternReplaceEnable; + private final boolean patternFullReplace; private final List patternReplaceSearchList; private final String patternReplaceWith; + private final String patternFullReplaceWith; public Log4j2Config(ProfilerConfig config) { @@ -45,9 +48,11 @@ public Log4j2Config(ProfilerConfig config) { this.patternReplaceSearchList = config.readList(LOGGING_PATTERN_REPLACE_SEARCH); this.patternReplaceWith = config.readString(LOGGING_PATTERN_REPLACE_WITH, ""); + this.patternFullReplaceWith = config.readString(LOGGING_PATTERN_FULL_REPLACE_WITH, ""); boolean configEnabled = config.readBoolean(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 isLog4j2LoggingTransactionInfo() { @@ -58,6 +63,10 @@ public boolean isPatternReplaceEnable() { return patternReplaceEnable; } + public boolean isPatternFullReplace() { + return patternFullReplace; + } + public List getPatternReplaceSearchList() { return patternReplaceSearchList; } @@ -66,13 +75,19 @@ public String getPatternReplaceWith() { return patternReplaceWith; } + public String getPatternFullReplaceWith() { + return patternFullReplaceWith; + } + @Override public String toString() { return "Log4j2Config{" + "log4j2LoggingTransactionInfo=" + log4j2LoggingTransactionInfo + ", patternReplaceEnable=" + patternReplaceEnable + + ", patternFullReplace='" + patternFullReplace + '\'' + ", patternReplaceSearchList=" + patternReplaceSearchList + ", patternReplaceWith='" + patternReplaceWith + '\'' + + ", patternFullReplaceWith='" + patternFullReplaceWith + '\'' + '}'; } diff --git a/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/interceptor/PatternLayoutInterceptor.java b/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/interceptor/PatternLayoutInterceptor.java index 5d337ab611d1..c6d8d3a08b5a 100644 --- a/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/interceptor/PatternLayoutInterceptor.java +++ b/agent-module/plugins/log4j2/src/main/java/com/navercorp/pinpoint/plugin/log4j2/interceptor/PatternLayoutInterceptor.java @@ -50,6 +50,10 @@ 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("Log4j2 pattern already have pinpoint pattern, pattern:{}", oldPattern); @@ -59,26 +63,39 @@ public void after(Object target, Object arg0, Object result, Throwable throwable updatePattern(target, oldPattern, config.getPatternReplaceSearchList(), config.getPatternReplaceWith()); } - private void updatePattern(Object target, String oldPattern, List searchList, String replace) { - if (!(target instanceof PatternLayout.SerializerBuilder)) { + private void updatePattern(Object target, String old, String replace) { + if (replace.contentEquals(old)) { return; } + if (updatePattern(target, replace)) { + logger.info("Log4j 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("Log4j2 pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern); - } break; } } if (changed) { - PatternLayout.SerializerBuilder builder = (PatternLayout.SerializerBuilder) target; - builder.setPattern(newPattern); + if(updatePattern(target, newPattern)) { + logger.info("Log4j2 pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern); + } + } + } + + private boolean updatePattern(Object target, String pattern) { + if (!(target instanceof PatternLayout.SerializerBuilder)) { + return false; } + PatternLayout.SerializerBuilder builder = (PatternLayout.SerializerBuilder) target; + builder.setPattern(pattern); + return true; } }