Log to a file on computer in Android

It’s been a long time since the last time I blogged about something, and many things have changed since then,
I graduated, got engaged and moved to Dubai.
I have plenty of things in my mind to blog about, many solutions and many tools that I developed that I’d like to share it with everyone, but I haven’t settled down, and my schedule is not stable yet, so I haven’t got enough time to sort things out and start blogging, anyway this post is simple and it doesn’t need much description so I thought I’d share it with you now.

Now let’s move to business, so I was working on an Android application and I needed to log some debug messages, normally I’d use Log.d(TAG, message); and this will send the message to logcat, but logcat is not useful for long messages, like for example if I needed to log the response I received from a web service, this response might be long and sending it to logcat will truncate it.

So usually in such situations we use files and log whatever we need inside them, but I’m working on an Android phone/emulator, and dealing with files and logging to them and reading them later from the computer will be a tedious thing to do, so I came up with a simple solution to log messages from Android to my computer.

The idea is to create a simple web server that accepts POST requests, these requests have only one parameter which is the data to be logged. I made it as simple as possible, I could have defined levels for logging and tags and other sorts of things, but I wanted a simple solution that wouldn’t take more than five minutes to write and get it working.

Simple web server

Since I’m a pythonist geek I couldn’t think of anything better than Python to implement the simple web server, and it was really great, since I didn’t have to write so much code!

I’ll be using HTTPServer and BaseHTTPRequestHandler from BaseHTTPServer module, some threading and cgi.

I set the server to run on port 5500, don’t ask why I chose this number, I didn’t think of it 😛
I define a function that will execute a function in a separate thread as a daemon thread:

I define the block of code to be executed when running the server:

Here I created an HTTP web server that is bound to the IP/PORT set at the beginning of the script, and I mapped the handler for this web server to a class called LogHandler.
This handler will handle the requests, and hence it’ll handle POST requests and save their contents into the specified file.

Pretty simple, not more than 35 lines of code!
Now let’s move to the Android/Java side.

Android/Java class

This class will be responsible for sending POST requests to the server, I’ll be using Apache classes to do the HTTP requests, and I’ll use some other classes from Java standard library, along with Android log class to log exceptions.

The class starts with defining some constants:

Then I define a method that constructs an HTTP POST request to the specified url along with the specified parameters:

I know this is a lot of code for such thing, but this method is used elsewhere in my application and that’s why it had to be this generic.

Another method will be used to execute the request, also it is generic to be used elsewhere, you can edit the code and make it more specific to your needs.

And now to the final piece of code here, this is the method that you’ll call from anywhere in your application to log messages to the file:

Download

The source code is licensed under the GNU Public License (GPL).
Project on GitHub

You can download the source code from here:
Source code from GitHub

Leave a Reply