'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 1:[1]

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.

Solution 2:[2]

I use System.getProperty("line.separator")

ArrayList<String> txts = new ArrayList<String>();
txts.add("aoeuaeou");
txts.add("snhsnthsnth");
String msg = TextUtils.join(System.getProperty("line.separator"),txts);
Log.d(TAG, "Bla bla bla: "+ msg );

show in the log like

Bla bla bla: aoeuaeou
snhsnthsnth

Solution 3:[3]

At the end of the message, a trailing space seems to be needed.

Log.i("tag", "My message with a blank line following.\n ");

or

Log.i("tag", "Variable 1: " + v1 + " Variable 2: " + v2 + "\n ");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 imaginabit
Solution 3