El "problema" es que si no quitamos (o comentamos) estos "menajes", también serán visibles para los usuarios de nuestras apps si consultan el Log en su terminal, o si miran el LogCat conectando su terminal a un PC, y esto no siempre queremos que ocurra.
Para evitar esto, en su tiempo cree una classe propia que permite mostrar/ocultar todos estos Logs cambiando sólo un parámetro, y la comparto con vosotros por si os resulta útil...
La classe en cuestión es esta:
public class MyLog {
private static final Boolean LOGS_ON = true;
// Send a DEBUG log message.
public static void d(String tag, String msg) {
if (LOGS_ON) {
Log.d(tag, msg);
}
}
// Send a DEBUG log message and log the exception.
public static void d(String tag, String msg, Throwable tr) {
if (LOGS_ON) {
Log.d(tag, msg, tr);
}
}
// Send an INFO log message.
public static void i(String tag, String msg) {
if (LOGS_ON) {
Log.i(tag, msg);
}
}
// Send a INFO log message and log the exception.
public static void i(String tag, String msg, Throwable tr) {
if (LOGS_ON) {
Log.i(tag, msg, tr);
}
}
// Send an ERROR log message.
public static void e(String tag, String msg) {
if (LOGS_ON) {
Log.e(tag, msg);
}
}
// Send a ERROR log message and log the exception.
public static void e(String tag, String msg, Throwable tr) {
if (LOGS_ON) {
Log.e(tag, msg, tr);
}
}
// Handy function to get a loggable stack trace from a Throwable
public static void getStackTraceString(Throwable tr) {
if (LOGS_ON) {
Log.getStackTraceString(tr);
}
}
// Send a VERBOSE log message.
public static void v(String tag, String msg) {
if (LOGS_ON) {
Log.v(tag, msg);
}
}
// Send a VERBOSE log message and log the exception.
public static void v(String tag, String msg, Throwable tr) {
if (LOGS_ON) {
Log.v(tag, msg, tr);
}
}
// Send a WARN log message.
public static void w(String tag, String msg) {
if (LOGS_ON) {
Log.w(tag, msg);
}
}
}
Y para usarla tan sólo tendremos que utilizar "MyLog.i(tag, msg)" en lugar de "Log.i(tag, msg)". Si queremos ocultar todos los logs, simplemente cambiamos el valor de la constante LOGS_ON a "false" y ya no se mostrará ningún Log.
No hay comentarios:
Publicar un comentario