http-client-branch: use a shared debug logger when debug logging is not enabled http-client-branch
authordfuchs
Thu, 26 Apr 2018 14:11:03 +0100
branchhttp-client-branch
changeset 56483 ff806eb4a2a4
parent 56482 5158cd0b906e
child 56486 b664a887e277
http-client-branch: use a shared debug logger when debug logging is not enabled
src/java.net.http/share/classes/jdk/internal/net/http/common/DebugLogger.java
--- a/src/java.net.http/share/classes/jdk/internal/net/http/common/DebugLogger.java	Thu Apr 26 10:16:13 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/common/DebugLogger.java	Thu Apr 26 14:11:03 2018 +0100
@@ -52,6 +52,12 @@
     final static System.Logger HTTP = System.getLogger(HTTP_NAME);
     final static System.Logger WS = System.getLogger(WS_NAME);
     final static System.Logger HPACK = System.getLogger(HPACK_NAME);
+    private static final DebugLogger NO_HTTP_LOGGER =
+            new DebugLogger(HTTP, "HTTP"::toString, Level.OFF, Level.OFF);
+    private static final DebugLogger NO_WS_LOGGER =
+            new DebugLogger(HTTP, "WS"::toString, Level.OFF, Level.OFF);
+    private static final DebugLogger NO_HPACK_LOGGER =
+            new DebugLogger(HTTP, "HPACK"::toString, Level.OFF, Level.OFF);
     final static long START_NANOS = System.nanoTime();
 
     private final Supplier<String> dbgTag;
@@ -112,6 +118,16 @@
     }
 
     private boolean isEnabled(Level level) {
+        return levelEnabledFor(level, outLevel, errLevel, logger);
+    }
+
+    @Override
+    public final boolean on() {
+        return debugOn;
+    }
+
+    static boolean levelEnabledFor(Level level, Level outLevel, Level errLevel,
+                                   System.Logger logger) {
         if (level == Level.OFF) return false;
         int severity = level.getSeverity();
         return severity >= errLevel.getSeverity()
@@ -120,11 +136,6 @@
     }
 
     @Override
-    public final boolean on() {
-        return debugOn;
-    }
-
-    @Override
     public boolean isLoggable(Level level) {
         // fast path, we assume these guys never change.
         // support only static configuration.
@@ -251,18 +262,33 @@
     public static DebugLogger createHttpLogger(Supplier<String> dbgTag,
                                                Level outLevel,
                                                Level errLevel) {
-        return new DebugLogger(HTTP, dbgTag, outLevel, errLevel);
+        if (levelEnabledFor(Level.DEBUG, outLevel, errLevel, HTTP)) {
+            return new DebugLogger(HTTP, dbgTag, outLevel, errLevel);
+        } else {
+            // return a shared instance if debug logging is not enabled.
+            return NO_HTTP_LOGGER;
+        }
     }
 
     public static DebugLogger createWebSocketLogger(Supplier<String> dbgTag,
                                                     Level outLevel,
                                                     Level errLevel) {
-        return new DebugLogger(WS, dbgTag, outLevel, errLevel);
+        if (levelEnabledFor(Level.DEBUG, outLevel, errLevel, WS)) {
+            return new DebugLogger(WS, dbgTag, outLevel, errLevel);
+        } else {
+            // return a shared instance if logging is not enabled.
+            return NO_WS_LOGGER;
+        }
     }
 
     public static DebugLogger createHpackLogger(Supplier<String> dbgTag,
                                                 Level outLevel,
                                                 Level errLevel) {
-        return new DebugLogger(HPACK, dbgTag, outLevel, errLevel);
+        if (levelEnabledFor(Level.DEBUG, outLevel, errLevel, HPACK)) {
+            return new DebugLogger(HPACK, dbgTag, outLevel, errLevel);
+        } else {
+            // return a shared instance if logging is not enabled.
+            return NO_HPACK_LOGGER;
+        }
     }
 }