Search code examples
javajakarta-eeloggingibm-cloud

Custom Log Handler using HTTP PUT Method and creates loop


i working on a Java Webservice. We want to build our own Custom Log Service on Bluemix. To use the log service i created a custom Log handler.

The log handler builds the request in the publish method and then try to send via http put method the log to the log service.

url = new URL(REQUEST_URL);         
connection = (HttpsURLConnection) url.openConnection();
//Content type
connection.setRequestProperty("Content-type", "application/json");
// Request Method
connection.setRequestMethod("PUT");
// return parameter
connection.setRequestProperty("Accept", "application/json");

if (body != null) {
  String length = Integer.toString(body.length());
  connection.setRequestProperty("Content-Length", length);
  connection.setDoOutput(true);
  connection.getOutputStream().write(body.getBytes("UTF8"));
}   

But if i use the level FINE or FINEST the HTTPURLConnection logs too and builds a inifinite loop.

Jun 03, 2015 8:01:52 AM sun.net.www.protocol.http.HttpURLConnection getInputStream
FINE: sun.net.www.MessageHeader@d423078012 pairs: {null: HTTP/1.1 202 Accepted}{X-Backside-Transport: OK OK}{Connection: Keep-Alive}{Transfer-Encoding: chunked}{Access-Control-Allow-Origin: *}{Content-Language: en-US}{Content-Type: application/json}{Date: Wed, 03 Jun 2015 08:01:52 GMT}{X-Cf-Requestid: ec6a22e8-123a-4699-6cf9-6d3d80818dda}{X-Powered-By: Servlet/3.1}{X-Client-IP: 184.172.29.74}{X-Global-Transaction-ID: 460768451}

Jun 03, 2015 8:01:51 AM sun.net.www.protocol.http.HttpURLConnection plainConnect
FINEST: Proxy used: DIRECT

Have i a logical missthinking or exisit a workaround for this problem?


Solution

  • A simple solution is to configure the logger not to log messages or use a higher level (such as INFO) for the URLConnection class or even its whole package. See http://docs.oracle.com/javase/7/docs/api/java/util/logging/LogManager.html on how to do this with java.util.logging.

    If your app is running on Liberty buildpack in Bluemix, you can also try to use Liberty's server.xml to configure the log levels, see http://www-01.ibm.com/support/knowledgecenter/SSD28V_8.5.5/com.ibm.websphere.wlp.core.doc/ae/rwlp_logging.html. You can push a server folder or server package to include this server.xml in your app package.