Skip to content

Commit

Permalink
[pinpoint-apm#11290] full logger pattern replace, log4j2
Browse files Browse the repository at this point in the history
  • Loading branch information
yjqg6666 committed Jul 26, 2024
1 parent cdb59a0 commit a57eaa7
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 @@ -1178,7 +1178,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ 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<String> patternReplaceSearchList;
private final String patternReplaceWith;
private final String patternFullReplaceWith;


public Log4j2Config(ProfilerConfig config) {
this.log4j2LoggingTransactionInfo = config.readBoolean(LOG4J2_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 isLog4j2LoggingTransactionInfo() {
Expand All @@ -58,6 +63,10 @@ public boolean isPatternReplaceEnable() {
return patternReplaceEnable;
}

public boolean isPatternFullReplace() {
return patternFullReplace;
}

public List<String> getPatternReplaceSearchList() {
return patternReplaceSearchList;
}
Expand All @@ -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 + '\'' +
'}';
}

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("Log4j2 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.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<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("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;
}

}

0 comments on commit a57eaa7

Please sign in to comment.