Question

I want to write some debugging output to the log to review it with logcat.

If I write something to System.out this is already displayed in logcat.

What is the clean way to write to the log and add levels and tags to my output?

Was it helpful?

Solution

Look into android.util.Log. It lets you write to the log with various log levels, and you can specify different tags to group the output. For example

Log.w("myApp", "no network");

will output a warning with the tag myApp and the message no network.

OTHER TIPS

The Tag is just used to easily find your output, because the Output of LogCat can be sometimes very long. You can define somewhere in your class:

private static final String TAG = "myApp";

and use it when debugging

Log.v(TAG, "did something");

enter image description here

You can apply as well a Filter to only search for the tag.

Use android.util.Log and the static methods defined there (e.g., e(), w()).

import android.util.Log;

and then

Log.i("the your message will go here"); 

Please see the logs as this way,

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)
String one = object.getdata();
Log.d(one,"");

Recently I found this approach to writing logs in android, which I think is super awesome.

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);
        }
    }

You can use my libary called RDALogger. Here is github link.

With this library, you can log your message with method name/class name/line number and anchor link. With this link, when you click log, screen goes to this line of code.

To use library, you must do implementations below.

in root level gradle

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

in app level gradle

dependencies {
            implementation 'com.github.ardakaplan:RDALogger:1.0.0'
    }

For initializing library, you should start like this (in Application.class or before first use)

RDALogger.start("TAG NAME").enableLogging(true);

And than you can log whatever you want;

    RDALogger.info("info");
    RDALogger.debug("debug");
    RDALogger.verbose("verbose");
    RDALogger.warn("warn");
    RDALogger.error("error");
    RDALogger.error(new Throwable());
    RDALogger.error("error", new Throwable());

And finally output shows you all you want (class name, method name, anchor link, message)

08-09 11:13:06.023 20025-20025/com.ardakaplan.application I/Application: IN CLASS : (ENApplication.java:29)   ///   IN METHOD : onCreate
    info
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top