< prev index next >

src/java.httpclient/share/classes/java/net/http/Log.java

Print this page




   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 
  28 /**
  29  * -Djava.net.HttpClient.log=errors,requests,headers,frames[:type:type2:..],content


  30  *
  31  * Any of errors, requests, headers or content are optional.
  32  *
  33  * Other handlers may be added. All logging is at level INFO
  34  *
  35  * Logger name is "java.net.http.HttpClient"
  36  */
  37 // implements System.Logger in order to be skipped when printing the caller's
  38 // information
  39 abstract class Log implements System.Logger {
  40 
  41     static final String logProp = "java.net.http.HttpClient.log";
  42 
  43     public static final int OFF = 0;
  44     public static final int ERRORS = 0x1;
  45     public static final int REQUESTS = 0x2;
  46     public static final int HEADERS = 0x4;
  47     public static final int CONTENT = 0x8;
  48     public static final int FRAMES = 0x10;
  49     public static final int SSL = 0x20;

  50     static int logging;
  51 
  52     // Frame types: "control", "data", "window", "all"
  53     public static final int CONTROL = 1; // all except DATA and WINDOW_UPDATES
  54     public static final int DATA = 2;
  55     public static final int WINDOW_UPDATES = 4;
  56     public static final int ALL = CONTROL| DATA | WINDOW_UPDATES;
  57     static int frametypes;
  58 
  59     static final System.Logger logger;
  60 
  61     static {
  62         String s = Utils.getNetProperty(logProp);
  63         if (s == null) {
  64             logging = OFF;
  65         } else {
  66             String[] vals = s.split(",");
  67             for (String val : vals) {
  68                 switch (val.toLowerCase(Locale.US)) {
  69                     case "errors":
  70                         logging |= ERRORS;
  71                         break;
  72                     case "requests":
  73                         logging |= REQUESTS;
  74                         break;
  75                     case "headers":
  76                         logging |= HEADERS;
  77                         break;
  78                     case "content":
  79                         logging |= CONTENT;
  80                         break;
  81                     case "ssl":
  82                         logging |= SSL;
  83                         break;



  84                     case "all":
  85                         logging |= CONTENT|HEADERS|REQUESTS|FRAMES|ERRORS;
  86                         break;
  87                 }
  88                 if (val.startsWith("frames")) {
  89                     logging |= FRAMES;
  90                     String[] types = val.split(":");
  91                     if (types.length == 1) {
  92                         frametypes = CONTROL | DATA | WINDOW_UPDATES;
  93                     } else {
  94                         for (String type : types) {
  95                             switch (type.toLowerCase()) {
  96                                 case "control":
  97                                     frametypes |= CONTROL;
  98                                     break;
  99                                 case "data":
 100                                     frametypes |= DATA;
 101                                     break;
 102                                 case "window":
 103                                     frametypes |= WINDOW_UPDATES;
 104                                     break;
 105                                 case "all":


 113         }
 114         if (logging != OFF) {
 115             logger = System.getLogger("java.net.http.HttpClient");
 116         } else {
 117             logger = null;
 118         }
 119     }
 120 
 121     static boolean errors() {
 122         return (logging & ERRORS) != 0;
 123     }
 124 
 125     static boolean requests() {
 126         return (logging & REQUESTS) != 0;
 127     }
 128 
 129     static boolean headers() {
 130         return (logging & HEADERS) != 0;
 131     }
 132 




 133     static boolean ssl() {
 134         return (logging & SSL) != 0;
 135     }
 136 
 137     static boolean frames() {
 138         return (logging & FRAMES) != 0;
 139     }
 140 
 141     static void logError(String s) {
 142         if (errors())
 143             logger.log(Level.INFO, "ERROR: " + s);
 144     }
 145 
 146     static void logError(Throwable t) {
 147         if (errors()) {
 148             String s = Utils.stackTrace(t);
 149             logger.log(Level.INFO, "ERROR: " + s);
 150         }
 151     }
 152 
 153     static void logSSL(String s) {
 154         if (ssl())
 155             logger.log(Level.INFO, "SSL: " + s);







 156     }
 157 
 158     static void logRequest(String s) {
 159         if (requests())
 160             logger.log(Level.INFO, "REQUEST: " + s);
 161     }
 162 
 163     static void logResponse(String s) {
 164         if (requests())
 165             logger.log(Level.INFO, "RESPONSE: " + s);
 166     }
 167 
 168     static void logHeaders(String s) {
 169         if (headers())
 170             logger.log(Level.INFO, "HEADERS: " + s);



















 171     }
 172 
 173     // not instantiable
 174     private Log() {}
 175 
 176 // END HTTP2
 177 }


   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 
  28 /**
  29  * -Djava.net.HttpClient.log=
  30  *          errors,requests,headers,
  31  *          frames[:type:type2:..],content,ssl,trace
  32  *
  33  * Any of errors, requests, headers or content are optional.
  34  *
  35  * Other handlers may be added. All logging is at level INFO
  36  *
  37  * Logger name is "java.net.http.HttpClient"
  38  */
  39 // implements System.Logger in order to be skipped when printing the caller's
  40 // information
  41 abstract class Log implements System.Logger {
  42 
  43     static final String logProp = "java.net.http.HttpClient.log";
  44 
  45     public static final int OFF = 0;
  46     public static final int ERRORS = 0x1;
  47     public static final int REQUESTS = 0x2;
  48     public static final int HEADERS = 0x4;
  49     public static final int CONTENT = 0x8;
  50     public static final int FRAMES = 0x10;
  51     public static final int SSL = 0x20;
  52     public static final int TRACE = 0x40;
  53     static int logging;
  54 
  55     // Frame types: "control", "data", "window", "all"
  56     public static final int CONTROL = 1; // all except DATA and WINDOW_UPDATES
  57     public static final int DATA = 2;
  58     public static final int WINDOW_UPDATES = 4;
  59     public static final int ALL = CONTROL| DATA | WINDOW_UPDATES;
  60     static int frametypes;
  61 
  62     static final System.Logger logger;
  63 
  64     static {
  65         String s = Utils.getNetProperty(logProp);
  66         if (s == null) {
  67             logging = OFF;
  68         } else {
  69             String[] vals = s.split(",");
  70             for (String val : vals) {
  71                 switch (val.toLowerCase(Locale.US)) {
  72                     case "errors":
  73                         logging |= ERRORS;
  74                         break;
  75                     case "requests":
  76                         logging |= REQUESTS;
  77                         break;
  78                     case "headers":
  79                         logging |= HEADERS;
  80                         break;
  81                     case "content":
  82                         logging |= CONTENT;
  83                         break;
  84                     case "ssl":
  85                         logging |= SSL;
  86                         break;
  87                     case "trace":
  88                         logging |= TRACE;
  89                         break;
  90                     case "all":
  91                         logging |= CONTENT|HEADERS|REQUESTS|FRAMES|ERRORS|TRACE;
  92                         break;
  93                 }
  94                 if (val.startsWith("frames")) {
  95                     logging |= FRAMES;
  96                     String[] types = val.split(":");
  97                     if (types.length == 1) {
  98                         frametypes = CONTROL | DATA | WINDOW_UPDATES;
  99                     } else {
 100                         for (String type : types) {
 101                             switch (type.toLowerCase()) {
 102                                 case "control":
 103                                     frametypes |= CONTROL;
 104                                     break;
 105                                 case "data":
 106                                     frametypes |= DATA;
 107                                     break;
 108                                 case "window":
 109                                     frametypes |= WINDOW_UPDATES;
 110                                     break;
 111                                 case "all":


 119         }
 120         if (logging != OFF) {
 121             logger = System.getLogger("java.net.http.HttpClient");
 122         } else {
 123             logger = null;
 124         }
 125     }
 126 
 127     static boolean errors() {
 128         return (logging & ERRORS) != 0;
 129     }
 130 
 131     static boolean requests() {
 132         return (logging & REQUESTS) != 0;
 133     }
 134 
 135     static boolean headers() {
 136         return (logging & HEADERS) != 0;
 137     }
 138 
 139     static boolean trace() {
 140         return (logging & TRACE) != 0;
 141     }
 142 
 143     static boolean ssl() {
 144         return (logging & SSL) != 0;
 145     }
 146 
 147     static boolean frames() {
 148         return (logging & FRAMES) != 0;
 149     }
 150 
 151     static void logError(String s, Object... s1) {
 152         if (errors())
 153             logger.log(Level.INFO, "ERROR: " + s, s1);
 154     }
 155 
 156     static void logError(Throwable t) {
 157         if (errors()) {
 158             String s = Utils.stackTrace(t);
 159             logger.log(Level.INFO, "ERROR: " + s);
 160         }
 161     }
 162 
 163     static void logSSL(String s, Object... s1) {
 164         if (ssl())
 165             logger.log(Level.INFO, "SSL: " + s, s1);
 166     }
 167 
 168     static void logTrace(String s, Object... s1) {
 169         if (trace()) {
 170             String format = "TRACE: " + s;
 171             logger.log(Level.INFO, format, s1);
 172         }
 173     }
 174 
 175     static void logRequest(String s, Object... s1) {
 176         if (requests())
 177             logger.log(Level.INFO, "REQUEST: " + s, s1);
 178     }
 179 
 180     static void logResponse(String s, Object... s1) {
 181         if (requests())
 182             logger.log(Level.INFO, "RESPONSE: " + s, s1);
 183     }
 184 
 185     static void logHeaders(String s, Object... s1) {
 186         if (headers())
 187             logger.log(Level.INFO, "HEADERS: " + s, s1);
 188     }
 189 // START HTTP2
 190     static boolean loggingFrame(Class<? extends Http2Frame> clazz) {
 191         if (frametypes == ALL) {
 192             return true;
 193         }
 194         if (clazz == DataFrame.class) {
 195             return (frametypes & DATA) != 0;
 196         } else if (clazz == WindowUpdateFrame.class) {
 197             return (frametypes & WINDOW_UPDATES) != 0;
 198         } else {
 199             return (frametypes & CONTROL) != 0;
 200         }
 201     }
 202 
 203     static void logFrames(Http2Frame f, String direction) {
 204         if (frames() && loggingFrame(f.getClass())) {
 205             logger.log(Level.INFO, "FRAME: " + direction + ": " + f.toString());
 206         }
 207     }
 208 
 209     // not instantiable
 210     private Log() {}
 211 
 212 // END HTTP2
 213 }
< prev index next >