jdk/src/java.httpclient/share/classes/java/net/http/Log.java
changeset 36131 379db4b2f95d
child 36665 8658fc0ab6e8
equal deleted inserted replaced
36130:5423259ef9ea 36131:379db4b2f95d
       
     1 /*
       
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  */
       
    24 package java.net.http;
       
    25 
       
    26 import java.util.Locale;
       
    27 import sun.util.logging.PlatformLogger;
       
    28 
       
    29 /**
       
    30  * -Djava.net.HttpClient.log=errors,requests,headers,frames[:type:type2:..],content
       
    31  *
       
    32  * Any of errors, requests, headers or content are optional.
       
    33  *
       
    34  * Other handlers may be added. All logging is at level INFO
       
    35  *
       
    36  * Logger name is "java.net.http.HttpClient"
       
    37  */
       
    38 class Log {
       
    39 
       
    40     final static String logProp = "java.net.http.HttpClient.log";
       
    41 
       
    42     public static final int OFF = 0;
       
    43     public static final int ERRORS = 0x1;
       
    44     public static final int REQUESTS = 0x2;
       
    45     public static final int HEADERS = 0x4;
       
    46     public static final int CONTENT = 0x8;
       
    47     public static final int FRAMES = 0x10;
       
    48     public static final int SSL = 0x20;
       
    49     static int logging;
       
    50 
       
    51     // Frame types: "control", "data", "window", "all"
       
    52     public static final int CONTROL = 1; // all except DATA and WINDOW_UPDATES
       
    53     public static final int DATA = 2;
       
    54     public static final int WINDOW_UPDATES = 4;
       
    55     public static final int ALL = CONTROL| DATA | WINDOW_UPDATES;
       
    56     static int frametypes;
       
    57 
       
    58     static sun.util.logging.PlatformLogger logger;
       
    59 
       
    60     static {
       
    61         String s = Utils.getNetProperty(logProp);
       
    62         if (s == null) {
       
    63             logging = OFF;
       
    64         } else {
       
    65             String[] vals = s.split(",");
       
    66             for (String val : vals) {
       
    67                 switch (val.toLowerCase(Locale.US)) {
       
    68                     case "errors":
       
    69                         logging |= ERRORS;
       
    70                         break;
       
    71                     case "requests":
       
    72                         logging |= REQUESTS;
       
    73                         break;
       
    74                     case "headers":
       
    75                         logging |= HEADERS;
       
    76                         break;
       
    77                     case "content":
       
    78                         logging |= CONTENT;
       
    79                         break;
       
    80                     case "ssl":
       
    81                         logging |= SSL;
       
    82                         break;
       
    83                     case "all":
       
    84                         logging |= CONTENT|HEADERS|REQUESTS|FRAMES|ERRORS;
       
    85                         break;
       
    86                 }
       
    87                 if (val.startsWith("frames")) {
       
    88                     logging |= FRAMES;
       
    89                     String[] types = val.split(":");
       
    90                     if (types.length == 1) {
       
    91                         frametypes = CONTROL | DATA | WINDOW_UPDATES;
       
    92                     } else {
       
    93                         for (String type : types) {
       
    94                             switch (type.toLowerCase()) {
       
    95                                 case "control":
       
    96                                     frametypes |= CONTROL;
       
    97                                     break;
       
    98                                 case "data":
       
    99                                     frametypes |= DATA;
       
   100                                     break;
       
   101                                 case "window":
       
   102                                     frametypes |= WINDOW_UPDATES;
       
   103                                     break;
       
   104                                 case "all":
       
   105                                     frametypes = ALL;
       
   106                                     break;
       
   107                             }
       
   108                         }
       
   109                     }
       
   110                 }
       
   111             }
       
   112         }
       
   113         if (logging != OFF) {
       
   114             logger = PlatformLogger.getLogger("java.net.http.HttpClient");
       
   115         }
       
   116     }
       
   117 
       
   118     static boolean errors() {
       
   119         return (logging & ERRORS) != 0;
       
   120     }
       
   121 
       
   122     static boolean requests() {
       
   123         return (logging & REQUESTS) != 0;
       
   124     }
       
   125 
       
   126     static boolean headers() {
       
   127         return (logging & HEADERS) != 0;
       
   128     }
       
   129 
       
   130     static boolean ssl() {
       
   131         return (logging & SSL) != 0;
       
   132     }
       
   133 
       
   134     static boolean frames() {
       
   135         return (logging & FRAMES) != 0;
       
   136     }
       
   137 
       
   138     static void logError(String s) {
       
   139         if (errors())
       
   140             logger.info("ERROR: " + s);
       
   141     }
       
   142 
       
   143     static void logError(Throwable t) {
       
   144         if (errors()) {
       
   145             String s = Utils.stackTrace(t);
       
   146             logger.info("ERROR: " + s);
       
   147         }
       
   148     }
       
   149 
       
   150     static void logSSL(String s) {
       
   151         if (ssl())
       
   152             logger.info("SSL: " + s);
       
   153     }
       
   154 
       
   155     static void logRequest(String s) {
       
   156         if (requests())
       
   157             logger.info("REQUEST: " + s);
       
   158     }
       
   159 
       
   160     static void logResponse(String s) {
       
   161         if (requests())
       
   162             logger.info("RESPONSE: " + s);
       
   163     }
       
   164 
       
   165     static void logHeaders(String s) {
       
   166         if (headers())
       
   167             logger.info("HEADERS: " + s);
       
   168     }
       
   169 // END HTTP2
       
   170 }