-2

Running PHP 7.0.1 I have come across a problem, I use error_log('....'.PHP_EOL.'...'); but when I look at the log file, I get verbatim \n in the output.

error_log is set up to use syslog, and ends up in apaches error_log. This is a CentOS 7 system.

Bypassing syslog is not an option as that removes the ability to use syslog forwarding to a central log server.

Andrea
  • 19,134
  • 4
  • 43
  • 65
Morten Nilsen
  • 621
  • 7
  • 18
  • Do you insist upon using syslog? If you define a file in php.ini, you can avoid this. http://stackoverflow.com/questions/1203664/php-error-log-and-newline-chars – Michael Berkowski Jan 03 '16 at 21:17
  • syslog forwarding is a facility I care about, so, yes. – Morten Nilsen Jan 03 '16 at 21:45
  • The single comma ' lacks variable interpolation. You should use double quotes. Please see: http://stackoverflow.com/questions/1402566/what-is-the-difference-between-and-in-php also see: http://docs.php.net/manual/en/language.types.string.php – Ryan Rentfro Jan 03 '16 at 21:54
  • 1
    variable interpolation is not relevant to this question. – Morten Nilsen Jan 03 '16 at 21:56
  • 1
    Oh, CentOS7 should be using rsyslog instead of plain old syslog.. Perhaps you can configure it: http://stackoverflow.com/questions/5463992/multiline-log-records-in-syslog – Michael Berkowski Jan 03 '16 at 22:00
  • Yes, that is the correct response to this question. – Morten Nilsen Jan 03 '16 at 22:03
  • The duplicate tag at the top points to the wrong answer :/ – Morten Nilsen Jan 03 '16 at 22:08
  • @MortenNilsen My original duplicate vote was 50 mins ago, but others just passed it through the queue. For most use cases besides yours, the linked question is probably appropriate (where remote syslog forwarding isn't used). Unfortunately I cannot recast a vote against the other rsyslog/python question which is now more appropriate. I can only vote to lift my original one. – Michael Berkowski Jan 03 '16 at 22:09
  • This also isn't a programming question then - it should have been posted on server fault to a group of sysadmin. Anytime you receive \n in your text you either haven't interpolated the output or your output was not supported by whatever you outputted to - it's black and white. Server OS doesn't matter in this case or the service handling it when the text comes across raw. – Ryan Rentfro Jan 03 '16 at 22:17
  • It can be hard to determine ahead of time where the issue is exactly. In this case, the answer is to either bypass syslog (php answer) or reconfigure syslog (server answer) - so it kind of fits in both camps, does it not? – Morten Nilsen Jan 03 '16 at 22:20

1 Answers1

-1

if you're worried about cross-os compatibility, just use "\r\n". Seems to be the better choice just about every time...

Also remember that \n in single-quotes ( '\n' ) will, indeed, print out the \n verbatim. double-quotes ( "\n" ) will get you what you want.

Jonathon Hibbard
  • 1,547
  • 13
  • 20