You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ck...@apache.org on 2021/09/13 18:44:36 UTC
[logging-log4j2] branch release-2.x updated: Minor cleanup in
AbstractLogger
This is an automated email from the ASF dual-hosted git repository.
ckozak pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new f7929c7 Minor cleanup in AbstractLogger
f7929c7 is described below
commit f7929c71b1e2633010ef97cc602754a710ad0d5a
Author: Carter Kozak <ck...@apache.org>
AuthorDate: Fri Sep 10 16:51:02 2021 -0400
Minor cleanup in AbstractLogger
Static threadlocal field `recursionDepthHolder` is final
Deduplication in `decrementRecursionDepth()`
handleLogMessageException uses interpolation to avoid overhead
when warnings are disabled
---
.../apache/logging/log4j/spi/AbstractLogger.java | 27 +++++++++-------------
1 file changed, 11 insertions(+), 16 deletions(-)
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
index e35d72a..b5114c8 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
@@ -110,7 +110,7 @@ public abstract class AbstractLogger implements ExtendedLogger, LocationAwareLog
protected final String name;
private final MessageFactory2 messageFactory;
private final FlowMessageFactory flowMessageFactory;
- private static ThreadLocal<int[]> recursionDepthHolder = new ThreadLocal<>(); // LOG4J2-1518, LOG4J2-2031
+ private static final ThreadLocal<int[]> recursionDepthHolder = new ThreadLocal<>(); // LOG4J2-1518, LOG4J2-2031
protected final transient ThreadLocal<DefaultLogBuilder> logBuilder;
/**
@@ -2174,11 +2174,11 @@ public abstract class AbstractLogger implements ExtendedLogger, LocationAwareLog
private static void incrementRecursionDepth() {
getRecursionDepthHolder()[0]++;
}
+
private static void decrementRecursionDepth() {
- final int[] depth = getRecursionDepthHolder();
- depth[0]--;
- if (depth[0] < 0) {
- throw new IllegalStateException("Recursion depth became negative: " + depth[0]);
+ int newDepth = --getRecursionDepthHolder()[0];
+ if (newDepth < 0) {
+ throw new IllegalStateException("Recursion depth became negative: " + newDepth);
}
}
@@ -2222,17 +2222,12 @@ public abstract class AbstractLogger implements ExtendedLogger, LocationAwareLog
if (exception instanceof LoggingException) {
throw (LoggingException) exception;
}
- final String format = message.getFormat();
- final int formatLength = format == null ? 4 : format.length();
- final StringBuilder sb = new StringBuilder(formatLength + 100);
- sb.append(fqcn);
- sb.append(" caught ");
- sb.append(exception.getClass().getName());
- sb.append(" logging ");
- sb.append(message.getClass().getSimpleName());
- sb.append(": ");
- sb.append(format);
- StatusLogger.getLogger().warn(sb.toString(), exception);
+ StatusLogger.getLogger().warn("{} caught {} logging {}: {}",
+ fqcn,
+ exception.getClass().getName(),
+ message.getClass().getSimpleName(),
+ message.getFormat(),
+ exception);
}
@Override