Skip to content

Commit feaf123

Browse files
michaelbraunvy
andauthored
Replace some DateTimeFormatter#toString() with #formatTo(StringBuilder) (#2515)
Co-authored-by: Volkan Yazıcı <[email protected]>
1 parent f37af11 commit feaf123

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ private static boolean appendDate(final Object o, final StringBuilder str) {
459459
if (!(o instanceof Date)) {
460460
return false;
461461
}
462-
str.append(DATE_FORMATTER.format(((Date) o).toInstant()));
462+
DATE_FORMATTER.formatTo(((Date) o).toInstant(), str);
463463
return true;
464464
}
465465

log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class StatusData implements Serializable {
3838

3939
private final Instant instant;
4040

41-
@Nullable
4241
private final DateTimeFormatter instantFormatter;
4342

4443
@Nullable
@@ -79,7 +78,8 @@ public StatusData(
7978
@Nullable final String threadName,
8079
@Nullable final DateTimeFormatter instantFormatter,
8180
final Instant instant) {
82-
this.instantFormatter = instantFormatter;
81+
// DateTimeFormatter.ISO_INSTANT is the default used in instant.toString()
82+
this.instantFormatter = instantFormatter != null ? instantFormatter : DateTimeFormatter.ISO_INSTANT;
8383
this.instant = instant;
8484
this.caller = caller;
8585
this.level = requireNonNull(level, "level");
@@ -167,9 +167,7 @@ public Throwable getThrowable() {
167167
@SuppressWarnings("DefaultCharset")
168168
public String getFormattedStatus() {
169169
final StringBuilder sb = new StringBuilder();
170-
final String formattedInstant =
171-
instantFormatter != null ? instantFormatter.format(instant) : instant.toString();
172-
sb.append(formattedInstant);
170+
instantFormatter.formatTo(instant, sb);
173171
sb.append(SPACE);
174172
sb.append(getThreadName());
175173
sb.append(SPACE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="2515" link="https://github.com/apache/logging-log4j2/pull/2515"/>
7+
<description format="asciidoc">Replace some usages of `DateTimeFormatter#toString()` with `DateTimeFormatter#formatTo(StringBuilder)` to cut down on allocations</description>
8+
</entry>

0 commit comments

Comments
 (0)