Skip to content

Commit

Permalink
[pinpoint-apm#11290] full logger pattern replace, log4j
Browse files Browse the repository at this point in the history
  • Loading branch information
yjqg6666 committed Nov 4, 2024
1 parent 11f3d06 commit 4b15bef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,9 @@ profiler.log4j.logging.transactioninfo=false
# variables/aliases: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
#profiler.log4j.logging.pattern.replace.enable=false
#profiler.log4j.logging.pattern.replace.search=%m
#profiler.log4j.logging.pattern.replace.with="TxId:%X{PtxId} %m"
#profiler.log4j.logging.pattern.replace.with=TxId:%X{PtxId} %m
# the logger pattern would be fully replaced with the configured value if replace.enable=true
#profiler.log4j.logging.pattern.full_replace.with=

###########################################################
# log4j2 (guide url : https://github.com/naver/pinpoint/blob/master/doc/per-request_feature_guide.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ public class Log4jConfig {
private static final String LOGGING_PATTERN_REPLACE_ENABLE = "profiler.log4j.logging.pattern.replace.enable";
private static final String LOGGING_PATTERN_REPLACE_SEARCH = "profiler.log4j.logging.pattern.replace.search";
private static final String LOGGING_PATTERN_REPLACE_WITH = "profiler.log4j.logging.pattern.replace.with";
private static final String LOGGING_PATTERN_FULL_REPLACE_WITH = "profiler.log4j.logging.pattern.full_replace.with";

private final boolean patternReplaceEnable;
private final List<String> patternReplaceSearchList;
private final String patternReplaceWith;
private final String patternFullReplaceWith;
private final boolean patternFullReplace;


public Log4jConfig(ProfilerConfig config) {
this.log4jLoggingTransactionInfo = config.readBoolean(LOG4J_LOGGING_TRANSACTION_INFO, false);

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 isLog4jLoggingTransactionInfo() {
Expand All @@ -63,13 +68,23 @@ public String getPatternReplaceWith() {
return patternReplaceWith;
}

public String getPatternFullReplaceWith() {
return patternFullReplaceWith;
}

public boolean isPatternFullReplace() {
return patternFullReplace;
}

@Override
public String toString() {
return "Log4jConfig{" +
"log4jLoggingTransactionInfo=" + log4jLoggingTransactionInfo +
", patternReplaceEnable=" + patternReplaceEnable +
", patternFullReplace='" + patternFullReplace + '\'' +
", patternReplaceSearchList=" + patternReplaceSearchList +
", patternReplaceWith='" + patternReplaceWith + '\'' +
", patternFullReplaceWith='" + patternFullReplaceWith + '\'' +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("Log4j pattern already have pinpoint pattern, pattern:{}", oldPattern);
Expand All @@ -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<String> searchList, String replace) {
if (!(target instanceof PatternLayout)) {
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<String> 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("Log4j pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern);
}
break;
}
}
if (changed) {
final PatternLayout layout = (PatternLayout) target;
layout.setConversionPattern(newPattern);
if (updatePattern(target, newPattern)) {
logger.info("Log4j pattern replaced, old pattern({}) and new pattern({})", oldPattern, newPattern);
}
}
}

private boolean updatePattern(Object target, String pattern) {
if (!(target instanceof PatternLayout)) {
return false;
}
final PatternLayout layout = (PatternLayout) target;
layout.setConversionPattern(pattern);
return true;
}

}

0 comments on commit 4b15bef

Please sign in to comment.