Writing to php://stderr

半城伤御伤魂 提交于 2019-12-07 05:13:44

问题


I'm trying to get the use of php://stderr for writing logs to work. I'm using Slim framework which makes use of @fopen('php://stderr', 'w') for logging and really want this to work.

The following test cases should work but only the first one does:

// 1. error_log - works fine
error_log("Written through the error_log function", 0);

// 2. PHP wrapper, ie php://stderr - does not work
$stderr = fopen( 'php://stderr', 'w' );
fwrite($stderr, "Written through the PHP error stream" );
fclose($stderr);


// 3. PHP wrapper also, different syntax, just to be safe - no effect either
file_put_contents( "php://stderr","Hello World" );


// 4. PHP wrapper, this time using this elusive constant referred to in the manual - result: "Notice: Use of undefined constant STDERR - assumed 'STDERR' ", ie: failed also!
file_put_contents( STDERR, "Hello World" );

I've been looking through the PHP manual and Googling a lot but without much help.

In particular, the following quote from the PHP manual on wrappers is confusing:

It is recommended that you simply use the constants STDIN, STDOUT and STDERR instead of manually opening streams using these [referring php://stdin, php://stdout and php://stderr] wrappers."

...given the undefined constant notice above. (I suspect those constants might be for use with PHP CLI -only?- but the page I'm citing does not state it.)

I've been wondering if this could be a Windows thing as I'm running XAMPP with PHP 5.3.8 for development but given the lack of topics on Google and the comments on PHP.net, I'm not so sure anymore. I do not have access to my production server logs right now for me to test out.


回答1:


Never mind, got it. I did not quite get the difference between php://stderr and error_log:

error_log writes to the PHP error log (eg: D:\xampp\php\logs\php_error_log)

php://stderr writes to the server/Apache error log (eg: D:\xampp\apache\logs\error.log)

Hopefully this helps someone else.




回答2:


If you are simply running PHP built-in web server (as of PHP 5.4.0) then php://stderr will be outputted to the screen of the console that launched PHP built-in web server.



来源:https://stackoverflow.com/questions/13830285/writing-to-php-stderr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!