Skip to content

Commit 587d34d

Browse files
vytheit
authored andcommitted
Fix concurrent date-time formatting issue in PatternLayout (apache#1485)
1 parent 441fd61 commit 587d34d

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-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
@@ -325,16 +325,15 @@ private Formatter getThreadLocalFormatter() {
325325
}
326326

327327
private void formatWithoutThreadLocals(final Instant instant, final StringBuilder output) {
328-
CachedTime cached = cachedTime.get();
328+
final CachedTime effective;
329+
final CachedTime cached = cachedTime.get();
329330
if (instant.getEpochSecond() != cached.epochSecond || instant.getNanoOfSecond() != cached.nanoOfSecond) {
330-
final CachedTime newTime = new CachedTime(instant);
331-
if (cachedTime.compareAndSet(cached, newTime)) {
332-
cached = newTime;
333-
} else {
334-
cached = cachedTime.get();
335-
}
331+
effective = new CachedTime(instant);
332+
cachedTime.compareAndSet(cached, effective);
333+
} else {
334+
effective = cached;
336335
}
337-
output.append(cached.formatted);
336+
output.append(effective.formatted);
338337
}
339338

340339
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
21+
type="fixed">
22+
<issue id="1485" link="https://github.com/apache/logging-log4j2/issues/1485"/>
23+
<author name="Stephan Markwalder" id="smarkwal"/>
24+
<author id="vy"/>
25+
<description format="asciidoc">Fix concurrent date-time formatting issue in `PatternLayout`</description>
26+
</entry>

0 commit comments

Comments
 (0)