Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP error_log outputting line breaks as literal "\n" strings on Mac OSX

I'm working on some code that uses error_log() for debugging. The problem is that for some reason, all of the line breaks in the output appear as literal \n strings, so there's no actual line break (making it damn near impossible to comprehend complex arrays).
I tried using both var_export and print_r with the error_log function, and both led to the same result: a block of text with \n scattered throughout. I even tried doing str_replace \n to PHP_EOL with no success. What am I doing wrong?

To clarify: the \n strings appear in the Console error log viewer, TextEdit, and the Terminal.

EDIT

Just wanted to note in advance that yes, I'm aware that you need to double-quote \n strings for them to appear as line breaks. However, I'm dealing with the output from print_r or var_export (to inspect an array) so this doesn't really help me (I think?).

like image 226
Matt Diamond Avatar asked Nov 29 '11 23:11

Matt Diamond


1 Answers

As an alternative, you can process / replace the '\n' via sed, so they display like new lines in the console:

Linux

$ tail -f error_log | sed "s/\\\n/\\n/g"

Mac OS X

# WARNING - don't just copy+paste (see comment below)
$ tail -f error_log | sed "s/\\\n/^M^L/g"

Note: In Mac OS X, the end of a line is CRLF (Carriage Return Line Feed) when in LINUX system it's only LF (Line Feed)

Note 2: In Mac OS X to output ^M in the Terminal, press Control+V Control+M, to output ^L, press Control+V Control+L

like image 136
eightyfive Avatar answered Sep 20 '22 02:09

eightyfive