author | dfuchs |
Wed, 23 Mar 2016 18:24:35 +0100 | |
changeset 36665 | 8658fc0ab6e8 |
parent 36131 | 379db4b2f95d |
child 37720 | 45cd7cc65382 |
permissions | -rw-r--r-- |
36131 | 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 |
||
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 |
*/ |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
37 |
// implements System.Logger in order to be skipped when printing the caller's |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
38 |
// information |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
39 |
abstract class Log implements System.Logger { |
36131 | 40 |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
41 |
static final String logProp = "java.net.http.HttpClient.log"; |
36131 | 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 |
||
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
59 |
static final System.Logger logger; |
36131 | 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": |
|
106 |
frametypes = ALL; |
|
107 |
break; |
|
108 |
} |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
if (logging != OFF) { |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
115 |
logger = System.getLogger("java.net.http.HttpClient"); |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
116 |
} else { |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
117 |
logger = null; |
36131 | 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()) |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
143 |
logger.log(Level.INFO, "ERROR: " + s); |
36131 | 144 |
} |
145 |
||
146 |
static void logError(Throwable t) { |
|
147 |
if (errors()) { |
|
148 |
String s = Utils.stackTrace(t); |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
149 |
logger.log(Level.INFO, "ERROR: " + s); |
36131 | 150 |
} |
151 |
} |
|
152 |
||
153 |
static void logSSL(String s) { |
|
154 |
if (ssl()) |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
155 |
logger.log(Level.INFO, "SSL: " + s); |
36131 | 156 |
} |
157 |
||
158 |
static void logRequest(String s) { |
|
159 |
if (requests()) |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
160 |
logger.log(Level.INFO, "REQUEST: " + s); |
36131 | 161 |
} |
162 |
||
163 |
static void logResponse(String s) { |
|
164 |
if (requests()) |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
165 |
logger.log(Level.INFO, "RESPONSE: " + s); |
36131 | 166 |
} |
167 |
||
168 |
static void logHeaders(String s) { |
|
169 |
if (headers()) |
|
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
170 |
logger.log(Level.INFO, "HEADERS: " + s); |
36131 | 171 |
} |
36665
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
172 |
|
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
173 |
// not instantiable |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
174 |
private Log() {} |
8658fc0ab6e8
8151281: Module java.httpclient could use System.Logger instead of PlatformLogger
dfuchs
parents:
36131
diff
changeset
|
175 |
|
36131 | 176 |
// END HTTP2 |
177 |
} |