Skip to content

Commit fea34c3

Browse files
committed
Fix concurrent date-time formatting issue in PatternLayout (#1485)
1 parent 53fc17c commit fea34c3

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/DatePatternConverter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,15 @@ private Formatter getThreadLocalFormatter() {
328328
}
329329

330330
private void formatWithoutThreadLocals(final Instant instant, final StringBuilder output) {
331-
CachedTime cached = cachedTime.get();
331+
final CachedTime effective;
332+
final CachedTime cached = cachedTime.get();
332333
if (instant.getEpochSecond() != cached.epochSecond || instant.getNanoOfSecond() != cached.nanoOfSecond) {
333-
final CachedTime newTime = new CachedTime(instant);
334-
if (cachedTime.compareAndSet(cached, newTime)) {
335-
cached = newTime;
336-
} else {
337-
cached = cachedTime.get();
338-
}
334+
effective = new CachedTime(instant);
335+
cachedTime.compareAndSet(cached, effective);
336+
} else {
337+
effective = cached;
339338
}
340-
output.append(cached.formatted);
339+
output.append(effective.formatted);
341340
}
342341

343342
/**

0 commit comments

Comments
 (0)