Fabien Snauwaert
Fabien Snauwaert

Reputation: 5651

Does writing to php://stderr write to logs under Apache?

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.

Upvotes: 15

Views: 25953

Answers (3)

DennisP
DennisP

Reputation: 168

You cannot file_put_contents into STDERR; STDERR is a stream resource (already opened). So you can only fwrite to STDERR. Please see https://www.php.net/manual/en/features.commandline.io-streams.php

Upvotes: 1

Paulius
Paulius

Reputation: 79

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.

Upvotes: 2

Fabien Snauwaert
Fabien Snauwaert

Reputation: 5651

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.

Upvotes: 19

Related Questions