Domanda

I wish to introduce some logging to an android application that I'm taking over. I wish to be able to reference this log file during developing, and to get a copy of the log if a customer has issues while the program is in production.

Since this would be in production, I would like the make the log file into a rolling log, so it doesn't consume too many resources.

In some android examples I noticed import android.util.Log;. I'm curious if this is a standard way of logging for android. If not, what is a good way of doing this (e.g. I really like log4j when coding in java.)?

  1. Can android.util.log do a rolling log like I'm requesting?

  2. Where does android.util.Log write to by default?

  3. If just to console, is there a way to write this to a rolling log file on the android device, which can then be emailed if there is a problem with the program?

È stato utile?

Soluzione

Can android.util.log do a rolling log like I'm requesting?

1) Not like you requested. The logs will be rolling but for all applications on the device, not just your app.

Where does android.util.Log write to by default?

2) The Android logging system, which can be viewed through logcat. Think of it like the Windows Event logs if you are familiar with that. I would download a logcat program and scroll through your system logs to familiarize yourself with them.

If just to console, is there a way to write this to a rolling log file on the android device, which can then be emailed if there is a problem with the program?

3) You could write your own rolling log to the filesystem and then email it. However, you would have to implement this functionality. You could also try seeing if there are any 3rd party logging tools/libraries that may satisfy your needs.

Altri suggerimenti

First of all. You should not enable heavy logging by default in your production code. We have solved it by writing an extension of the default log class as below.

With it the logging is default on in development code and default off in production code. You may write some small hidden enabler so that your customer may activate logging when neccessary.

Secondly use a app for storing and retrieving logs! Try CatLog or similar.

Good luck!

public class MyLog {

    private static boolean m_Enabled = BuildConfig.DEBUG;

    public MyLog() {

    }

    public static void e(String tag, String msg) {
        if (m_Enabled) Log.e(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (m_Enabled) Log.w(tag, msg);
    }

    public static void i(String tag, String msg) {
        if (m_Enabled) Log.i(tag, msg);
    }

    public static void d(String tag, String msg) {
        if (m_Enabled) Log.d(tag, msg);
    }

    public static void v(String tag, String msg) {
        if (m_Enabled) Log.v(tag, msg);
    }

    public static void enable() {
        m_Enabled = true;
    }

    public static void disable() {
        m_Enabled = false;
    }

    public static boolean isEnabled() {
       return m_Enabled;
    }
}

There is documentation, and examples, of how to do a size-bounded rolling log on the android device using the standard Log.X Android call & "logCat" - for example see here and here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top