< prev index next >
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=
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":
112 frametypes = ALL;
113 break;
114 }
115 }
116 }
117 }
118 }
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 >