This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new feaf12326d Replace some `DateTimeFormatter#toString()` with
`#formatTo(StringBuilder)` (#2515)
feaf12326d is described below
commit feaf12326da6b17d1986d5a4c71f0ea48463c6ee
Author: Michael Braun <[email protected]>
AuthorDate: Fri Apr 26 02:18:05 2024 -0400
Replace some `DateTimeFormatter#toString()` with `#formatTo(StringBuilder)`
(#2515)
Co-authored-by: Volkan Yazıcı <[email protected]>
---
.../java/org/apache/logging/log4j/message/ParameterFormatter.java | 2 +-
.../src/main/java/org/apache/logging/log4j/status/StatusData.java | 8 +++-----
src/changelog/.2.x.x/2515_datetimeformatter_formatto.xml | 8 ++++++++
3 files changed, 12 insertions(+), 6 deletions(-)
diff --git
a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
index ac96a1736f..0e990894c0 100644
---
a/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
+++
b/log4j-api/src/main/java/org/apache/logging/log4j/message/ParameterFormatter.java
@@ -459,7 +459,7 @@ final class ParameterFormatter {
if (!(o instanceof Date)) {
return false;
}
- str.append(DATE_FORMATTER.format(((Date) o).toInstant()));
+ DATE_FORMATTER.formatTo(((Date) o).toInstant(), str);
return true;
}
diff --git
a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
index a56cf0e9f9..337224459a 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/status/StatusData.java
@@ -38,7 +38,6 @@ public class StatusData implements Serializable {
private final Instant instant;
- @Nullable
private final DateTimeFormatter instantFormatter;
@Nullable
@@ -79,7 +78,8 @@ public class StatusData implements Serializable {
@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");
@@ -167,9 +167,7 @@ public class StatusData implements Serializable {
@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);
diff --git a/src/changelog/.2.x.x/2515_datetimeformatter_formatto.xml
b/src/changelog/.2.x.x/2515_datetimeformatter_formatto.xml
new file mode 100644
index 0000000000..b8cc41d1b2
--- /dev/null
+++ b/src/changelog/.2.x.x/2515_datetimeformatter_formatto.xml
@@ -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>