Android Logging Strings with newline character or <br>
Asked Answered
L

4

7

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?

Lecialecithin answered 8/9, 2014 at 15:40 Comment(0)
K
10

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.

Kirmess answered 8/9, 2014 at 15:52 Comment(1)
Stackoverflow also formatted my <br> away... I actually have a string with has a valid <br>Lecialecithin
C
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
Coprophilia answered 8/2, 2016 at 12:4 Comment(0)
O
1

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 ");
Outsoar answered 14/4, 2022 at 21:31 Comment(0)
E
0

In case of anyone has problems displaying newlines in logcat from Xamarin Android. Visual Studio displays incorrectly logs from logcat. I have spent a lot of time looking for problem with logging and the problem was with displaying logs. Just use different tool. The Android Studio displays logs correctly.

Enterotomy answered 1/3, 2023 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.