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 2020/11/19 15:37:37 UTC

[logging-log4j2] branch release-2.x updated: Align JSON template layout code base with master.

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

vy pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 1c340db  Align JSON template layout code base with master.
1c340db is described below

commit 1c340db374e6cc5627582469bcdb2d0cf3352136
Author: Volkan Yazici <vo...@gmail.com>
AuthorDate: Thu Nov 19 15:26:21 2020 +0100

    Align JSON template layout code base with master.
---
 log4j-layout-template-json/pom.xml                 |  51 +++---
 .../template/json/JsonTemplateLayoutDefaults.java  | 178 ++++++---------------
 .../template/json/resolver/ExceptionResolver.java  |   2 +-
 .../template/json/resolver/TemplateResolver.java   |  27 ++++
 .../layout/template/json/util/JsonWriterTest.java  |   2 -
 5 files changed, 109 insertions(+), 151 deletions(-)

diff --git a/log4j-layout-template-json/pom.xml b/log4j-layout-template-json/pom.xml
index e9a46a3..a2fce5f 100644
--- a/log4j-layout-template-json/pom.xml
+++ b/log4j-layout-template-json/pom.xml
@@ -383,18 +383,19 @@
                         <arg>1</arg>
                         <arg>-e</arg>
                         <arg>
+                          <![CDATA[
                           input {
                             gelf {
-                              host =&gt; "logstash"
-                              use_tcp =&gt; true
-                              use_udp =&gt; false
-                              port =&gt; 12222
-                              type =&gt; "gelf"
+                              host => "logstash"
+                              use_tcp => true
+                              use_udp => false
+                              port => 12222
+                              type => "gelf"
                             }
                             tcp {
-                              port =&gt; 12345
-                              codec =&gt; json
-                              type =&gt; "tcp"
+                              port => 12345
+                              codec => json
+                              type => "tcp"
                             }
                           }
 
@@ -403,17 +404,17 @@
                               # These are GELF/Syslog logging levels as defined in RFC 3164.
                               # Map the integer level to its human readable format.
                               translate {
-                                field =&gt; "[level]"
-                                destination =&gt; "[levelName]"
-                                dictionary =&gt; {
-                                  "0" =&gt; "EMERG"
-                                  "1" =&gt; "ALERT"
-                                  "2" =&gt; "CRITICAL"
-                                  "3" =&gt; "ERROR"
-                                  "4" =&gt; "WARN"
-                                  "5" =&gt; "NOTICE"
-                                  "6" =&gt; "INFO"
-                                  "7" =&gt; "DEBUG"
+                                field => "[level]"
+                                destination => "[levelName]"
+                                dictionary => {
+                                  "0" => "EMERG"
+                                  "1" => "ALERT"
+                                  "2" => "CRITICAL"
+                                  "3" => "ERROR"
+                                  "4" => "WARN"
+                                  "5" => "NOTICE"
+                                  "6" => "INFO"
+                                  "7" => "DEBUG"
                                 }
                               }
                             }
@@ -421,12 +422,13 @@
 
                           output {
                             # (Un)comment for debugging purposes
-                            # stdout { codec =&gt; rubydebug }
+                            # stdout { codec => rubydebug }
                             elasticsearch {
-                              hosts =&gt; ["http://elasticsearch:9200"]
-                              index =&gt; "log4j"
+                              hosts => ["http://elasticsearch:9200"]
+                              index => "log4j"
                             }
                           }
+                          ]]>
                         </arg>
                       </exec>
                     </entrypoint>
@@ -501,6 +503,11 @@
       </plugin>
 
       <plugin>
+        <groupId>com.github.spotbugs</groupId>
+        <artifactId>spotbugs-maven-plugin</artifactId>
+      </plugin>
+
+      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jxr-plugin</artifactId>
         <version>${jxr.plugin.version}</version>
diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
index 9896c1e..4f417ee 100644
--- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
+++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutDefaults.java
@@ -29,71 +29,7 @@ public enum JsonTemplateLayoutDefaults {;
 
     private static final PropertiesUtil PROPERTIES = PropertiesUtil.getProperties();
 
-    private static final Charset CHARSET = readCharset();
-
-    private static final boolean LOCATION_INFO_ENABLED =
-            PROPERTIES.getBooleanProperty(
-                    "log4j.layout.jsonTemplate.locationInfoEnabled",
-                    false);
-
-    private static final boolean STACK_TRACE_ENABLED =
-            PROPERTIES.getBooleanProperty(
-                    "log4j.layout.jsonTemplate.stackTraceEnabled",
-                    true);
-
-    private static final String TIMESTAMP_FORMAT_PATTERN =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.timestampFormatPattern",
-                    "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
-
-    private static final TimeZone TIME_ZONE = readTimeZone();
-
-    private static final Locale LOCALE = readLocale();
-
-    private static final String EVENT_TEMPLATE =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.eventTemplate");
-
-    private static final String EVENT_TEMPLATE_URI =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.eventTemplateUri",
-                    "classpath:EcsLayout.json");
-
-    private static final String STACK_TRACE_ELEMENT_TEMPLATE =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.stackTraceElementTemplate");
-
-    private static final String STACK_TRACE_ELEMENT_TEMPLATE_URI =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.stackTraceElementTemplateUri",
-                    "classpath:StackTraceElementLayout.json");
-
-    private static final String MDC_KEY_PATTERN =
-            PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.mdcKeyPattern");
-
-    private static final String NDC_PATTERN =
-            PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.ndcPattern");
-
-    private static final String EVENT_DELIMITER =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.eventDelimiter",
-                    System.lineSeparator());
-
-    private static final boolean NULL_EVENT_DELIMITER_ENABLED =
-            PROPERTIES.getBooleanProperty(
-                    "log4j.layout.jsonTemplate.nullEventDelimiterEnabled",
-                    false);
-
-    private static final int MAX_STRING_LENGTH = readMaxStringLength();
-
-    private static final String TRUNCATED_STRING_SUFFIX =
-            PROPERTIES.getStringProperty(
-                    "log4j.layout.jsonTemplate.truncatedStringSuffix",
-                    "…");
-
-    private static final RecyclerFactory RECYCLER_FACTORY = readRecyclerFactory();
-
-    private static Charset readCharset() {
+    public static Charset getCharset() {
         final String charsetName =
                 PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.charset");
         return charsetName != null
@@ -101,7 +37,25 @@ public enum JsonTemplateLayoutDefaults {;
                 : StandardCharsets.UTF_8;
     }
 
-    private static TimeZone readTimeZone() {
+    public static boolean isLocationInfoEnabled() {
+        return PROPERTIES.getBooleanProperty(
+                "log4j.layout.jsonTemplate.locationInfoEnabled",
+                false);
+    }
+
+    public static boolean isStackTraceEnabled() {
+        return PROPERTIES.getBooleanProperty(
+                "log4j.layout.jsonTemplate.stackTraceEnabled",
+                true);
+    }
+
+    public static String getTimestampFormatPattern() {
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.timestampFormatPattern",
+                "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ");
+    }
+
+    public static TimeZone getTimeZone() {
         final String timeZoneId =
                 PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.timeZone");
         return timeZoneId != null
@@ -109,7 +63,7 @@ public enum JsonTemplateLayoutDefaults {;
                 : TimeZone.getDefault();
     }
 
-    private static Locale readLocale() {
+    public static Locale getLocale() {
         final String locale =
                 PROPERTIES.getStringProperty("log4j.layout.jsonTemplate.locale");
         if (locale == null) {
@@ -124,90 +78,62 @@ public enum JsonTemplateLayoutDefaults {;
         }
     }
 
-    private static int readMaxStringLength() {
-        final int maxStringLength = PROPERTIES.getIntegerProperty(
-                "log4j.layout.jsonTemplate.maxStringLength",
-                16 * 1_024);
-        if (maxStringLength <= 0) {
-            throw new IllegalArgumentException(
-                    "was expecting a non-zero positive maxStringLength: " +
-                            maxStringLength);
-        }
-        return maxStringLength;
-    }
-
-    private static RecyclerFactory readRecyclerFactory() {
-        final String recyclerFactorySpec = PROPERTIES.getStringProperty(
-                "log4j.layout.jsonTemplate.recyclerFactory");
-        return RecyclerFactories.ofSpec(recyclerFactorySpec);
-    }
-
-    public static Charset getCharset() {
-        return CHARSET;
-    }
-
-    public static boolean isLocationInfoEnabled() {
-        return LOCATION_INFO_ENABLED;
-    }
-
-    public static boolean isStackTraceEnabled() {
-        return STACK_TRACE_ENABLED;
-    }
-
-    public static String getTimestampFormatPattern() {
-        return TIMESTAMP_FORMAT_PATTERN;
-    }
-
-    public static TimeZone getTimeZone() {
-        return TIME_ZONE;
-    }
-
-    public static Locale getLocale() {
-        return LOCALE;
-    }
-
     public static String getEventTemplate() {
-        return EVENT_TEMPLATE;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.eventTemplate");
     }
 
     public static String getEventTemplateUri() {
-        return EVENT_TEMPLATE_URI;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.eventTemplateUri",
+                "classpath:EcsLayout.json");
     }
 
     public static String getStackTraceElementTemplate() {
-        return STACK_TRACE_ELEMENT_TEMPLATE;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.stackTraceElementTemplate");
     }
 
     public static String getStackTraceElementTemplateUri() {
-        return STACK_TRACE_ELEMENT_TEMPLATE_URI;
-    }
-
-    public static String getMdcKeyPattern() {
-        return MDC_KEY_PATTERN;
-    }
-
-    public static String getNdcPattern() {
-        return NDC_PATTERN;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.stackTraceElementTemplateUri",
+                "classpath:StackTraceElementLayout.json");
     }
 
     public static String getEventDelimiter() {
-        return EVENT_DELIMITER;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.eventDelimiter",
+                System.lineSeparator());
     }
 
     public static boolean isNullEventDelimiterEnabled() {
-        return NULL_EVENT_DELIMITER_ENABLED;
+        return PROPERTIES.getBooleanProperty(
+                "log4j.layout.jsonTemplate.nullEventDelimiterEnabled",
+                false);
     }
 
     public static int getMaxStringLength() {
-        return MAX_STRING_LENGTH;
+        final int maxStringLength = PROPERTIES.getIntegerProperty(
+                "log4j.layout.jsonTemplate.maxStringLength",
+                16 * 1_024);
+        if (maxStringLength <= 0) {
+            throw new IllegalArgumentException(
+                    "was expecting a non-zero positive maxStringLength: " +
+                            maxStringLength);
+        }
+        return maxStringLength;
     }
 
     public static String getTruncatedStringSuffix() {
-        return TRUNCATED_STRING_SUFFIX;
+        return PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.truncatedStringSuffix",
+                "…");
     }
 
     public static RecyclerFactory getRecyclerFactory() {
-        return RECYCLER_FACTORY;
+        final String recyclerFactorySpec = PROPERTIES.getStringProperty(
+                "log4j.layout.jsonTemplate.recyclerFactory");
+        return RecyclerFactories.ofSpec(recyclerFactorySpec);
     }
 
 }
diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolver.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolver.java
index 4cd9b62..415104a 100644
--- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolver.java
+++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/ExceptionResolver.java
@@ -22,7 +22,7 @@ import org.apache.logging.log4j.layout.template.json.util.JsonWriter;
 
 /**
  * Exception resolver.
- *
+ * <p>
  * Note that this resolver is toggled by {@link
  * JsonTemplateLayout.Builder#setStackTraceEnabled(boolean)}.
  *
diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolver.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolver.java
index 5db3813..62ce41d 100644
--- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolver.java
+++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolver.java
@@ -21,20 +21,47 @@ import org.apache.logging.log4j.layout.template.json.util.JsonWriter;
 @FunctionalInterface
 public interface TemplateResolver<V> {
 
+    /**
+     * Indicates if the resolution should be appended to the parent JSON object.
+     * <p>
+     * For instance, {@link ThreadContextDataResolver}, i.e., MDC resolver,
+     * uses this flag to indicate whether the contents should be appended to the
+     * parent JSON object or not.
+     */
     default boolean isFlattening() {
         return false;
     }
 
+    /**
+     * Indicates if the resolver if applicable at all.
+     * <p>
+     * For instance, the source line resolver can be short-circuited using this
+     * check if the location information is disabled in the layout configuration.
+     */
     default boolean isResolvable() {
         return true;
     }
 
+    /**
+     * Indicates if the resolver if applicable for the given {@code value}.
+     * <p>
+     * For instance, the stack trace resolver can be short-circuited using this
+     * check if the stack traces are disabled in the layout configuration.
+     */
     default boolean isResolvable(V value) {
         return true;
     }
 
+    /**
+     * Resolves the given {@code value} using the provided {@link JsonWriter}.
+     */
     void resolve(V value, JsonWriter jsonWriter);
 
+    /**
+     * Resolves the given {@code value} using the provided {@link JsonWriter}.
+     *
+     * @param succeedingEntry false, if this is the first element in a collection; true, otherwise
+     */
     default void resolve(V value, JsonWriter jsonWriter, boolean succeedingEntry) {
         resolve(value, jsonWriter);
     }
diff --git a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonWriterTest.java b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonWriterTest.java
index 68fe638..def4d42 100644
--- a/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonWriterTest.java
+++ b/log4j-layout-template-json/src/test/java/org/apache/logging/log4j/layout/template/json/util/JsonWriterTest.java
@@ -375,7 +375,6 @@ public class JsonWriterTest {
     @Test
     public void test_writeString_formattable() {
         final String expectedJson = "\"foo\\tbar\\tbuzz\"";
-        @SuppressWarnings("Convert2Lambda")
         final String actualJson = WRITER.use(() ->
                 WRITER.writeString(stringBuilder -> stringBuilder.append("foo\tbar\tbuzz")));
         Assertions.assertThat(actualJson).isEqualTo(expectedJson);
@@ -389,7 +388,6 @@ public class JsonWriterTest {
                 excessiveString.substring(0, maxStringLength) +
                 WRITER.getTruncatedStringSuffix() +
                 '"';
-        @SuppressWarnings("Convert2Lambda")
         final String actualJson = WRITER.use(() ->
                 WRITER.writeString(stringBuilder -> stringBuilder.append(excessiveString)));
         Assertions.assertThat(actualJson).isEqualTo(expectedJson);