Skip to content

Commit

Permalink
[pinpoint-apm#11290] full logger pattern replace, logback
Browse files Browse the repository at this point in the history
  • Loading branch information
yjqg6666 committed Nov 4, 2024
1 parent 5e7b3bb commit 355d332
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,27 @@ 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<String> patternReplaceSearchList;
private final String patternReplaceWith;
private final String patternFullReplaceWith;


public LogbackConfig(ProfilerConfig config) {
this.logbackLoggingTransactionInfo = config.readBoolean(LOGBACK_LOGGING_TRANSACTION_INFO, false);

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() {
Expand All @@ -57,6 +62,10 @@ public boolean isPatternReplaceEnable() {
return patternReplaceEnable;
}

public boolean isPatternFullReplace() {
return patternFullReplace;
}

public List<String> getPatternReplaceSearchList() {
return patternReplaceSearchList;
}
Expand All @@ -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 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> 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<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("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;
}

}

0 comments on commit 355d332

Please sign in to comment.