Tuesday, July 27, 2010

Custom Logger for Android

Now and then you must feel the need to have some more control over the way Android Application logs are getting displayed. Possible requirement scenarios; redirecting your Android logs into a File or you might want to control the logs those are getting displayed e.g. for production mode, you may want to block all logs to get better performance.
I have written a custom Logger, which is a wrapper over android.util.Log. This will keep existing behavior of Log class as it is and add few handy features to get more control over your application logs-
Storing Logs directly into File- at present all logs comes on the LogCat. You need to manually copy/past logs from LogCat to any file for further analysis. Another option is to get the Dump state when an application gets “Forced Closed”. But this wrapper will allow you an automatic solution. Imagine a scenario, your Phone is not connected with the LogCat and you need the tracing. This wrapper will be helpful in this scenario.
Controlled Logging- it will give you the control to select the range of logs to be display/stored e.g. if you select the allowed Log Level to VERBOSE, all Log Level (integer values) higher than VERBOSE will be displayed. You can even completely block all logging (perfect for production).
To get above additional benefits, you need to refer to a new Class instead of android.util.Log-
MLog.i(“TAG”, “ Log Message”)
All methods are static and make sure, you call MLog.init(context, “ Log_File_Name”) at the very beginning (probably in the onCreate() method of your Activity). This utility class will create a log file under data/data/ Your_Application_Package/files/ Log_File_Name
Sample usage scenarios-
Step1- Configure the Logger (Optional Step)
To stop logging-
MLog.enableLog = false; (this will be suitable for Production Release)
To enable logging only of INFO, WARN and ERROR-
MLog.logLevel = MLog.INFO;
Step2- Initialize the Logger
Once you set above properties, initiate the logger-
MLog.init(this.getApplicationContext(), "app_log.txt");
Note: be careful to pass Application Context rather than Activity Context, which might cause Leak Window issue.
Step3- Use the Logger. Same as you use Log.i(TAG, msg), Log.v(TAG, msg)
MLog.i(TAG, "Location Activity...");
So now you can get these additional benefits by using the attached Logger (MLog.java) in your next project. Have a happy and controlled logging :-)
  

9 comments:

  1. Hi Prasanta,

    Awesome blog.. hats off to u man.


    I `ve a problem regarding this Logger.
    I `ve followed all the steps u mentioned above.
    but the Logfile.txt isn`t getting generated.
    i don`t any error.

    Thanks,
    Sameera

    ReplyDelete
  2. Hi Sameera,
    thanks for your comment.

    Make sure your are following this-
    [To Call in your code]-
    Initialize-
    MLog.init(this.getApplicationContext(), "mystery.log");
    Once initialization is done, you can call-
    MLog.i(TAG, "Start listening Phone State...");

    File will be generated under-
    (DDMS File Explorer) data/data/[Your_Project_Package]/files/mystery.log

    Hope it helps.
    Thanks.

    ReplyDelete
  3. Thanks for ur quick response..

    I have called init method

    MLog.init(this.getApplicationContext(), "mystery.log");

    and i `ve checked that {(DDMS File Explorer) data/data/[Your_Project_Package]/files/mystery.log } but file isn`t created.

    i wanna know the extension to b given.
    Is is .txt or .log??

    I `ve tried with both but no file is generated..:(

    Thanks
    Sameera

    ReplyDelete
  4. Hi Sameera,

    can you send your source? I'll look into that.
    Thanks.

    ReplyDelete
  5. You cannot see the files on your mobile. You don't have access to the data directory.

    If you want to see the files, you have to write them to the sd card of the mobile.

    Rohan

    ReplyDelete
  6. Hi, Prasanta

    I wanted to learn more about this MLog class but unfortunately I am not able to download the code from the above link for MLog.java.

    Can you please re-share the link for your logger.

    Thanks

    ReplyDelete
  7. the link has error 404
    can u share again?

    another q: if i want to put parameter indicates if this is debug mode or not, where can i put it and ask about it in my logger?

    thanks

    ReplyDelete
  8. Hello ,

    I really like your Logger post, but unfortunately your MLog.java link is broken. Would really appreciate it if you can provide us with a new link.

    Thank you.
    Georges

    ReplyDelete
  9. http://www.java2s.com/Open-Source/Android-Open-Source-App/Sample/BeautifulWidgets_SkinMixer_Android/com/andexp/skinmixer/utils/MLog.java.htm

    ReplyDelete