PHP Error log file format (php.ini error_log directive) on Windows
Asked Answered
W

1

6

For an example:
php.ini file

...
; Log errors to specified file.
error_log = c:/php/php.log
...

Error log file (c:/php/php.log) contains every entry in this format:

[12-Jun-2011 12:58:55] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 12:59:01] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:01:12] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:02:11] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:11:23] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:12:10] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n

Two carriage return character and one new line per one error line.

Why it happens? How to change error log file to default format:

[12-Jun-2011 12:58:55] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 12:59:01] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:01:12] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:02:11] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:11:23] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:12:10] PHP Notice:  Undefined variable: test in C:\www\phpinfo.php on line 2\r\n

PHP Version 5.3.6
Apache/2.2.15 (Win32)
Tested on Windows 7 Home Basic and Windows XP SP3, same results.

php.ini file contains only two strings

log_errors = On
error_log = c:/server/php.log

apache phpinfo() script -> http://pastehtml.com/view/awvx1vgpp.html

PS.

sever: nginx 1.0.4
FastCGI + PHP Version 5.3.6

Everything works as expected.
nginx phpinfo() script -> http://pastehtml.com/view/awvwvk9p9.html

Winkle answered 12/6, 2011 at 10:29 Comment(5)
Are you sure this is \n\n and not \r\n <- single line break on windows?Substantiate
It's for an example. \r\r\n between each error line now.Winkle
Please add the PHP version and the version of the operating system at the end of your question.Substantiate
I have this same problem with Apache 2.2.22 and PHP 5.4.3 on Windows 7 Home Premium x64. Either I can't use html error formatting in any case (display or log). html_errors have no effect.Lambeth
It seems to be a bug in the Windows build of PHP that has finally been fixed in PHP 7.1 but not 7.0 or earlier. Simply swapping in PHP 7.1.11 in place of 7.0.25 on the same system (Windows 7, but also reproduced on Windows 10 and equally fixed by upgrade to PHP 7.1.9), same Apache (2.4.29), same php.ini diffs from the shipped php.ini.development, no other differences whatsoever, and the problem is fixed.Tucky
S
2

There are more ini directives to control the output into the error log. These are explained on this PHP manual page.

By default, PHP running with not loading the ini and only having the error_log set and logging enabled, it does output a single line only. You can test that on your system as well, it demonstrates it well for the Command Line Interface (CLI) "Server Application Programming Interface (API) [SAPI]":

php -n -d error_log=./error.log -d log_errors=1 -r 'error;'

Use this command to make an isolated run and compare it with your output. If it has two line endings as well, then this is probably a bug of your php version.

If not a problem with the PHP version [not the case in the original posting (OP)] then within your application some settings looks to be altered. You need to find out which setting. Probably you can find it by registering an own error handler and then dumping the ini settings.

Last but not least, if it isn't a misunderstanding of the line ending (see my comment), then one may want to also look in the ini settings named error_prepend_string and error_append_string regarding the error display (but not the error logging).

Substantiate answered 12/6, 2011 at 10:34 Comment(21)
Yeah, i just have checked these ini directives, but issue still exists.Winkle
Okay my test here was on a very similar system. PHP 5.3.6, Win XP. Tested in CLI, not via webserver. I've updated the answer.Substantiate
Fine, then it's something within the script and/or the configuration it runs in. Is this a known application or something you've coded?Substantiate
Okay, lol. Let's try this: Please add var_dump(array_map('ini_get', array('error_prepend_string', 'error_append_string', 'auto_prepend_file'))); before the erroneous line. I try to reproduce on a apache with FCGI as well.Substantiate
results: array(3) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" }Winkle
Another possibility could be some PHP module as well. the php -n did prevent to load module as well (as none were configured). Just saying to broaden the view.Substantiate
Hmm, that should have actually returned the values of the ini settings. Weird, I don't see the problem.Substantiate
I've updated my question with php.ini and phpinfo() information.Winkle
I can't open the image you've linked, can you use a host that works w/o javascript? I think SE has one if you add an image to your post and it offers thumbnails.Substantiate
For those looking at this answer in 2021. error_prepend_string and error_append_string straight up haven't worked in PHP since at least 2014Costmary
@Shardj: Looking at your comment in 2021 I was curious if that is the case: php8.0 -n -derror_prepend_string="error:" -derror_append_string=$':error\n' -r 'trigger_error("Message"); printf("PHP Version: %s\n", PHP_VERSION);' error: Notice: Message in Command line code on line 1 :error PHP Version: 8.0.2Substantiate
Strange, it hasn't been working at all for me, and I found a PHP bug reported in 2014 stating that it doesn't do anything. So I figured it just doesn't work any more. I wonder if it's an issue with PHP-FPM specifically then in my case since it works fine on the CLI with the code you provided.Costmary
Yeah, doesn't work either in ini config or with php's ini_set for either FPM or CLI. However it does work when using -derror_prepend_string="error:" when running php from bash directly like in your command.Costmary
@Shardj: That is CLI, just used it for the demonstration. Please share a link to the bugreport so we can look into it.Substantiate
Yeah I'm aware, but setting the config through the CLI ini file doesn't work for me. I'm saying that running it through the cli with the -r flag such as your example does work. And I have no idea why those behave differently.Costmary
@Shardj: If you would share the link to the bug report I might be able to give better directions.Substantiate
I doubt the old bug report I found has any relation to the issue I'm having nowCostmary
@Costmary well you mentioned it so I thought there was relevance. So what is your suggestion with your problem? Any idea?Substantiate
Well for my solution I've avoided the issue by changing to using fluentd to send my logs, and I'm just prefixing with that. As for the the PHP issue no clueCostmary
@Shardj: Yes, that is an entirely different level. Wasn't aware you had problems to configure Fluentd. Take care that when you set the prefix (or postfix) in PHP error logging, the prefix and postifx are separated by a newline character from the error message. So it might not have been what you were looking for with these settings in any case, just saying.Substantiate
@Shardj: Reviewing the answer I can see a benefit in clarifying the identity of the error log, especially for the append and prepend directives. their output is the error output, which is the error display and not the error log.Substantiate

© 2022 - 2024 — McMap. All rights reserved.