You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/03/14 21:04:50 UTC
[logging-log4j2] 10/17: LOG4J2-3393 Inline context acquisition in JsonTemplateLayout.
This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch LOG4J2-3393
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit f5b172e9b4525042b3205e3d3b0fafb803f9a419
Author: Volkan Yazici <vo...@yazi.ci>
AuthorDate: Mon Feb 21 21:48:52 2022 +0100
LOG4J2-3393 Inline context acquisition in JsonTemplateLayout.
---
.../layout/template/json/JsonTemplateLayout.java | 30 +++++++++++-----------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
index 8747c98..5e36817 100644
--- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
+++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
@@ -73,8 +73,7 @@ public class JsonTemplateLayout implements StringLayout {
private final Recycler<Context> contextRecycler;
- // The class and fields are visible for tests.
- static final class Context implements AutoCloseable {
+ private static final class Context implements AutoCloseable {
final JsonWriter jsonWriter;
@@ -259,36 +258,42 @@ public class JsonTemplateLayout implements StringLayout {
@Override
public String toSerializable(final LogEvent event) {
- final Context context = acquireContext();
+
+ // Acquire a context.
+ final Recycler<Context> contextRecycler = this.contextRecycler;
+ final Context context = contextRecycler.acquire();
final JsonWriter jsonWriter = context.jsonWriter;
final StringBuilder stringBuilder = jsonWriter.getStringBuilder();
+
+ // Render the JSON.
try {
eventResolver.resolve(event, jsonWriter);
stringBuilder.append(eventDelimiter);
return stringBuilder.toString();
- } finally {
+ }
+
+ // Release the context.
+ finally {
contextRecycler.release(context);
}
+
}
@Override
public void encode(final LogEvent event, final ByteBufferDestination destination) {
// Acquire a context.
- final Context context = acquireContext();
+ final Recycler<Context> contextRecycler = this.contextRecycler;
+ final Context context = contextRecycler.acquire();
final JsonWriter jsonWriter = context.jsonWriter;
final StringBuilder stringBuilder = jsonWriter.getStringBuilder();
final Encoder<StringBuilder> encoder = context.encoder;
+ // Render & write the JSON.
try {
-
- // Render the JSON.
eventResolver.resolve(event, jsonWriter);
stringBuilder.append(eventDelimiter);
-
- // Write to the destination.
encoder.encode(stringBuilder, destination);
-
}
// Release the context.
@@ -298,11 +303,6 @@ public class JsonTemplateLayout implements StringLayout {
}
- // Visible for tests.
- Context acquireContext() {
- return contextRecycler.acquire();
- }
-
@Override
public byte[] getFooter() {
return null;