Christian
Christian

Reputation: 26387

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?

Upvotes: 7

Views: 11847

Answers (4)

raV720
raV720

Reputation: 594

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.

Upvotes: 0

Jon
Jon

Reputation: 565

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

Upvotes: 1

imaginabit
imaginabit

Reputation: 429

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

Upvotes: 2

Paul Ratazzi
Paul Ratazzi

Reputation: 6397

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.

Upvotes: 10

Related Questions