¿Cómo escribo salidas en el Inicio de sesión en Android?
Quiero escribir una salida de depuración en el registro para revisarlo con logcat.
Si escribo algo al Sistema.out esto ya se muestra en logcat.
¿Cuál es la forma limpia de escribir en el registro y agregar niveles y etiquetas a mi salida?
7 answers
Mira en android.util.Log
. Le permite escribir en el registro con varios niveles de registro, y puede especificar diferentes etiquetas para agrupar la salida.
Por ejemplo
Log.w("myApp", "no network");
Emitirá una advertencia con la etiqueta myApp y el mensaje no network.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-14 13:32:51
La etiqueta solo se usa para encontrar fácilmente su salida, porque la salida de LogCat puede ser a veces muy larga. Puedes definir en algún lugar de tu clase:
Etiqueta de cadena final estática privada = "myApp";
Y usarlo al depurar
Log.v (TAG, "hizo algo");
También puede aplicar un filtro para buscar solo la etiqueta.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-14 06:11:47
Use android.util.Log
y los métodos estáticos definidos allí (e. g., e()
, w()
).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-02 16:36:56
import android.util.Log;
Y luego
Log.i("the your message will go here");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-17 06:26:07
Por favor, vea los registros de esta manera,
Log.e("ApiUrl = ", "MyApiUrl") (error)
Log.w("ApiUrl = ", "MyApiUrl") (warning)
Log.i("ApiUrl = ", "MyApiUrl") (information)
Log.d("ApiUrl = ", "MyApiUrl") (debug)
Log.v("ApiUrl = ", "MyApiUrl") (verbose)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-20 04:22:07
String one = object.getdata();
Log.d(one,"");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-23 19:53:00
Recientemente encontré este enfoque para escribir registros en Android, que creo que es súper impresionante.
public static final boolean FORCED_LOGGING = true;
private static final int CALLER_STACK_INDEX = 3;
public static void showLogs(String message) {
if (FORCED_LOGGING) {
StackTraceElement caller = Thread.currentThread().getStackTrace()[CALLER_STACK_INDEX];
String fullClassName = caller.getClassName();
String className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
String methodName = caller.getMethodName();
int lineNumber = caller.getLineNumber();
Log.i("*** " + className + "." + methodName + "():" + lineNumber + "\n" , message);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-20 04:12:43