viernes, 11 de febrero de 2011

Android: my custom log class

I use a custom log class on all my Android projects to easy my life.

Advantages
  • Possibility to enable/disable log by configuration. Useful to keep your releases clean.
  • Class call trace. Know which class is displaying which log message with no extra text.

How it's done?

Really easy. Just create a new class:
class Log {
public final static String LOGTAG = "Phone2Chrome";
static final boolean LOGV = true;

static void v(String logMe) {
 if (LOGV) {
  try {
   throw new Exception("go go logger");
  } catch (Exception e) {
   String className = e.getStackTrace()[1].getClassName();
   className = className.substring(className.lastIndexOf(".")+1, className.length());
   android.util.Log.v(LOGTAG, className + "." + e.getStackTrace()[1].getMethodName() + "() --- " + logMe);
  }
 }
}

Import it in your Android classes and make this replacement:
void myFunction() {
- Log.d(TAG, "MyActivity - myFunction: hello logcat");
+ Log.v("hello logcat");
}

What you are going to see in logcat is something like this:

V/Phone2Chrome(16119): MyActivity.myFuntion() --- hello logcat.

No more dirty log messages anymore!

4 comentarios:

  1. Hi!
    I think you've two mistakes. For example, where is the "d" method in your Log class? It's a great idea to fill the entire stack trace in your log method not only your method and to write your debug message into the log, not avoid it.
    By the way, I'm gonna test your app, seems great, but I don't know why are you using Dropbox to share the URL in the Android device ¿? Why don't use another service in the cloud?
    Best regards,

    ResponderEliminar
  2. Thanks for reviewing this ;)

    There is no "d" method in my custom log class. I simply use:

    Log.v("Point 2 reached");

    to log stuff. And to swtich off logs in production environment I just change global constant LOGV from true to false

    And, about Dropbox, I'm using it because it's free and has a good mobile and web API. It's not very difficult to build services around.

    And very important, it's not Chrome or Android dependent. That allows me (or someone) to write a Firefox extension for my app ;)

    ResponderEliminar
  3. Ok,

    I understand your point. You are hidding the debug method of your log class, that's the point, isn't?

    That's useful but you're losing one level of logging. If you don't needed don't worry about it, I don't think could be important, unless you need, for example, to make different testing approaches in different environments or if you are concerned about the security (finally you're only one setting variable away to allow to write all of your logs to the user logcat)

    The bad approach then is the idea of throwing an Exception in the 'v' method, it's a lot of computing CPU (the stack is adding few points and reference to restore it) for no reason. Could be a better solution to add another parameter to the class reference and then in the same method use the classic getter for obtain the class where the object is instanciated (i.e object.getClass())

    About the use of Dropbox, everybody knows that is a great service, but my question was about, why to add this requirement (yes, I know, it's free to create a new account), but why to use this instead of think another solution, that doesn't implies to having previously an account in Dropbox. For example, more than two people I know are using other services in the cloud with the same great satisfaction about the great service.

    Anyway, a great idea and good blog, I'll stay following you ;-)

    Best regards and good luck!

    ResponderEliminar
  4. Gracias Nacho, te contesto en castellano que me parece que nos va a resultar más cómodo a los dos ;)

    Voy a echar un ojo al tema del consumo de cpu por lanzar excepciones. Puede ser algo a tener muy en cuenta. Aún así sólo lo tengo activo cuando depuro el código, no debería afectar a la app una vez subida al Market.

    Sobre Dropbox en realidad lo uso como podría usar cualquier otro servicio de almacenamiento en la nube. La app necesita algún sitio donde guardar el fichero para sincronizar con la extensión. Dropbox fue mi elección por exponer una API fácil de usar y por estar muy extendido.

    Umm tengo que hacer un post sobre la API Javascript que hice para conectar... :)

    ResponderEliminar