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 2024/04/16 08:45:11 UTC

(logging-log4j2) branch main updated: Address cache misses in `LevelResolver`

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 97e441dd9e Address cache misses in `LevelResolver`
97e441dd9e is described below

commit 97e441dd9ee4236fd1ba31bc373bebee483a2ddc
Author: Volkan Yazıcı <vo...@yazi.ci>
AuthorDate: Tue Apr 16 10:44:19 2024 +0200

    Address cache misses in `LevelResolver`
---
 .../template/json/resolver/LevelResolver.java      | 32 ++++++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/LevelResolver.java b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/LevelResolver.java
index 7ecae03c4c..c346864371 100644
--- a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/LevelResolver.java
+++ b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/LevelResolver.java
@@ -88,11 +88,19 @@ public final class LevelResolver implements EventResolver {
 
     private static final EventResolver SEVERITY_CODE_RESOLVER =
             (final LogEvent logEvent, final JsonWriter jsonWriter) -> {
-                final int standardLevelOrdinal =
-                        logEvent.getLevel().getStandardLevel().ordinal();
+                final Level level = logEvent.getLevel();
+                final int standardLevelOrdinal = level.getStandardLevel().ordinal();
                 final String severityCodeResolution =
                         SEVERITY_CODE_RESOLUTION_BY_STANDARD_LEVEL_ORDINAL[standardLevelOrdinal];
-                jsonWriter.writeRawString(severityCodeResolution);
+                if (severityCodeResolution != null) {
+                    jsonWriter.writeRawString(severityCodeResolution);
+                }
+                // `Level.values()` can change at runtime and cause a lookup miss.
+                // When that happens, fallback to the slow path:
+                else {
+                    final int severityCode = Severity.getSeverity(level).getCode();
+                    jsonWriter.writeNumber(severityCode);
+                }
             };
 
     private final EventResolver internalResolver;
@@ -132,7 +140,10 @@ public final class LevelResolver implements EventResolver {
             final String resolution = resolutionByLevel.get(level);
             if (resolution != null) {
                 jsonWriter.writeRawString(resolution);
-            } else {
+            }
+            // `Level.values()` can change at runtime and cause a lookup miss.
+            // When that happens, fallback to the slow path:
+            else {
                 final String levelName = level.name();
                 jsonWriter.writeString(levelName);
             }
@@ -149,8 +160,17 @@ public final class LevelResolver implements EventResolver {
                             contextJsonWriter.writeString(severityKeyword);
                         })));
         return (final LogEvent logEvent, final JsonWriter jsonWriter) -> {
-            final String resolution = resolutionByLevel.get(logEvent.getLevel());
-            jsonWriter.writeRawString(resolution);
+            final Level level = logEvent.getLevel();
+            String resolution = resolutionByLevel.get(level);
+            if (resolution != null) {
+                jsonWriter.writeRawString(resolution);
+            }
+            // `Level.values()` can change at runtime and cause a lookup miss.
+            // When that happens, fallback to the slow path:
+            else {
+                final String severityKeyword = Severity.getSeverity(level).name();
+                jsonWriter.writeString(severityKeyword);
+            }
         };
     }