androidandroid-logcat

Android Logging Strings with newline character or <br>


It seems that if you call

String text = "String<br>String";
Log.d(TAG, text);

it automatically parses the String to take two lines. The same goes for new line (\n) characters. That makes debugging more complicated. Is there a way to tell the logger to give me the exact String?


Solution

  • The arguments to the methods in Log class are Java strings, so escaping special characters is just like in Java. For example,

    String text = "String\nString";
    Log.d("TEST!!", text);
    

    Will give you:

    D/TEST!!﹕ String
        String
    

    while:

    String text = "String\\nString";
    Log.d("TEST!!", text);
    

    will give you:

    D/TEST!!﹕ String\nString
    

    in the logcat.

    As far as <BR>, I'm not seeing the same effect as you. Specifically,

    String text = "String<br>String";
    Log.d("TEST!!", text);
    

    Produces:

    D/TEST!!﹕ String<br>String
    

    So I am unable to reproduce your actual problem. However, in general special characters in the Log strings are escaped just like any other Java strings. The logger is dumb and there's no settings to automatically escape special characters; you'll have to do this yourself for arbitrary strings. The Log methods just turn around and call println_native.