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/02/21 20:49:11 UTC

[logging-log4j2] branch LOG4J2-3393 updated (37edca9 -> 510606e)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a change to branch LOG4J2-3393
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


    from 37edca9  LOG4J2-3393 Avoid StringBuilderEncoder thread-local allocation in JsonTemplateLayout.
     new e7d4004  LOG4J2-3393 Remove redundant branching in JsonTemplateLayout.
     new 510606e  LOG4J2-3393 Inline context acquisition in JsonTemplateLayout.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../layout/template/json/JsonTemplateLayout.java   | 38 +++++++++-------------
 1 file changed, 16 insertions(+), 22 deletions(-)

[logging-log4j2] 01/02: LOG4J2-3393 Remove redundant branching in JsonTemplateLayout.

Posted by vy...@apache.org.
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 e7d40044157c0e19f9a1e89d1a0f1b8615c80237
Author: Volkan Yazici <vo...@yazi.ci>
AuthorDate: Mon Feb 21 20:43:12 2022 +0100

    LOG4J2-3393 Remove redundant branching in JsonTemplateLayout.
---
 .../logging/log4j/layout/template/json/JsonTemplateLayout.java    | 8 +-------
 1 file changed, 1 insertion(+), 7 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 a585bc5..8747c98 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
@@ -287,13 +287,7 @@ public class JsonTemplateLayout implements StringLayout {
             stringBuilder.append(eventDelimiter);
 
             // Write to the destination.
-            if (encoder == null) {
-                final String eventJson = stringBuilder.toString();
-                final byte[] eventJsonBytes = StringEncoder.toBytes(eventJson, charset);
-                destination.writeBytes(eventJsonBytes, 0, eventJsonBytes.length);
-            } else {
-                encoder.encode(stringBuilder, destination);
-            }
+            encoder.encode(stringBuilder, destination);
 
         }
 

[logging-log4j2] 02/02: LOG4J2-3393 Inline context acquisition in JsonTemplateLayout.

Posted by vy...@apache.org.
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 510606eab351270ecc646bbf7f88ca105f9dca84
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;