AWS Mobile Blog

Logging with the AWS SDK for Android

by Bob Kinney | on | Permalink | Comments |  Share

Logging can be an effective way to debug your application or library, capturing information that can help diagnose errors in logic or verify assumptions of data consistency. The AWS SDK for Android includes logging via the Apache Commons logging project. The SDK makes use of this library and its link to [http://developer.android.com/tools/help/logcat.html Android LogCat] to provide logging for internal operations of the SDK. By default, only informational, warning, and error messages will be visible by LogCat, but in this post I’ll discuss how you can control the level of logging generated by the SDK.

Disabling All Logging

If you want to disable ”’all”’ logging in the SDK, you simply need to insert the following piece of code in your Android app, perhaps in your initial Activity’s onCreate method:

java.util.logging.Logger.getLogger("com.amazonaws").setLevel(java.util.logging.Level.OFF);

Enabling Request Logging

Outside of the basic informational and error logging, the SDK can also offer basic request and response logging when talking to AWS services. This logging is useful to debug successful requests that are not producing the desired result, and you may be asked to enable it if you contact AWS support. Enabling this logging requires two steps; first, you need to add the following code to your Android app:

java.util.logging.Logger.getLogger("com.amazonaws").setLevel(java.util.logging.Level.FINEST);

Make sure you remove this code before exporting your app for use in production as it will effect your app’s performance.

Secondly, you need to execute the following [http://developer.android.com/tools/help/adb.html Adroid Debug Bridge] (ADB) shell command to enable LogCat to display these new log messages:

adb shell setprop log.tag.com.amazonaws.request DEBUG

You must run this command either with your Android device connected and configured for development or with your AVD running in the emulator. This setting persists only until the emulator/device restarts.

Enabling HTTP Client Logging

If you need to debug further down in the internals of the SDK (either at the request of AWS support or for your own interests), you can log the raw HTTP requests and responses via Apache HTTP client logging. To enable this logging, you must first add the following code to your app:

java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.FINEST);

As with request logging, make sure you remove this code before exporting your app for use in production as it will effect your app’s performance.

Secondly, you need to execute the following ADB shell commands to enable LogCat to display these new log messages:

adb shell setprop log.tag.org.apache.http.headers VERBOSE 
adb shell setprop log.tag.org.apache.http.wire VERBOSE

You must run these commands either with your Android device connected and configured for development or with your AVD running in the emulator. This setting persists only until the emulator/device restarts.

As always, if you have any questions, please feel free to leave a comment below or post in our forums.

If you like building mobile applications that use cloud services that our customers use on a daily basis, perhaps you would like to join the AWS Mobile SDK and Tools team. We are hiring Software Developers, Web Developers, and Product Managers.