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

10,291

Solution 1

Apparently, this issue is independent of your operating system. See this question: PHP error log and newline chars

...you should be able to change the error_log directive in your php.ini on Debian to point to a file. If this is undefined, it will go through syslog which doesn't support multiple lines.

Solution 2

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

Share:
10,291

Related videos on Youtube

Matt Diamond
Author by

Matt Diamond

As a web developer and musician, I have a keen interest in the intersection of sound and technology. If you have work involving web audio, I'm interested. My current pet project is AskOuija, a subreddit where questions are answered one letter at a time. In addition to creating and styling the subreddit, I coded a bot in Javascript to scan threads and compile answers. I created the original version of RecorderJS, a useful javascript plugin for capturing and exporting WAV audio from the Web Audio API. I also built FuckItJS, which some people seem to enjoy.

Updated on June 07, 2022

Comments

  • Matt Diamond
    Matt Diamond about 2 years

    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?).

  • Chris Burgess
    Chris Burgess over 11 years
    Note also that for Apache to write newlines to error.log, it will need write permissions to do so (else syslog will end up writing to error.log instead). Perms fix or new file 'php-errors.log' if you want to preserve syslog format for Apache's errors (my preference).