Data & beer; Beer & data.

Enabling HTTP request / response logging in the Google APIs Client Library for Java

Posted: June 3rd, 2014 | Author: | Filed under: Blog | Tags: , , , | No Comments »

This one usually gets me every time I touch this library, so I’m posting my notes for future reference.

Logging can be enabled by creating a custom HttpRequestInitializer and HttpResponseInterceptor, but that will consume the InputStream and you’ll likely see getContent() return null, or IOExceptionStream closed” errors.

However, the client library uses Java’s standard java.util.logging platform, which can be enabled to write to the console / stderr with these steps.

  • Create a file, called say ‘logging.properties’ and store it somewhere accessible to your project (I just dump it in the project root for debugging).
  • Paste these lines into the file:
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=CONFIG
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
  • Run your app with this extra VM argument, by either adding it to your command line when you run java -cp … -Djava.util.logging… MyClass, or to the “VM options” field in your IDE.
-Djava.util.logging.config.file=logging.properties

Et voila, logs!



Leave a Reply