¿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?

Author: Daniel Lew, 2010-03-02

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.

 190
Author: Erich Douglass,
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");

introduzca la descripción de la imagen aquí

También puede aplicar un filtro para buscar solo la etiqueta.

 19
Author: user1767754,
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()).

 17
Author: CommonsWare,
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"); 
 4
Author: Atreya Rath,
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)
 2
Author: Dnyaneshwar Panchal,
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,"");
 0
Author: Vickie Kangare,
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);
        }
    }
 0
Author: Tushar Pandey,
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