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?).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With