Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private static boolean appendDate(final Object o, final StringBuilder str) {
if (!(o instanceof Date)) {
return false;
}
str.append(DATE_FORMATTER.format(((Date) o).toInstant()));
DATE_FORMATTER.formatTo(((Date) o).toInstant(), str);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class StatusData implements Serializable {

private final Instant instant;

@Nullable
private final DateTimeFormatter instantFormatter;

@Nullable
Expand Down Expand Up @@ -79,7 +78,8 @@ public StatusData(
@Nullable final String threadName,
@Nullable final DateTimeFormatter instantFormatter,
final Instant instant) {
this.instantFormatter = instantFormatter;
// DateTimeFormatter.ISO_INSTANT is the default used in instant.toString()
this.instantFormatter = instantFormatter != null ? instantFormatter : DateTimeFormatter.ISO_INSTANT;
this.instant = instant;
this.caller = caller;
this.level = requireNonNull(level, "level");
Expand Down Expand Up @@ -167,9 +167,7 @@ public Throwable getThrowable() {
@SuppressWarnings("DefaultCharset")
public String getFormattedStatus() {
final StringBuilder sb = new StringBuilder();
final String formattedInstant =
instantFormatter != null ? instantFormatter.format(instant) : instant.toString();
sb.append(formattedInstant);
instantFormatter.formatTo(instant, sb);
sb.append(SPACE);
sb.append(getThreadName());
sb.append(SPACE);
Expand Down
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/2515_datetimeformatter_formatto.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="changed">
<issue id="2515" link="https://github.com/apache/logging-log4j2/pull/2515"/>
<description format="asciidoc">Replace some usages of `DateTimeFormatter#toString()` with `DateTimeFormatter#formatTo(StringBuilder)` to cut down on allocations</description>
</entry>