Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to php://stderr

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.

like image 917
Fabien Snauwaert Avatar asked Dec 11 '12 23:12

Fabien Snauwaert


People also ask

Where does PHP stderr write to?

By default, whenever an error or exception is thrown, PHP sends the error message directly to the user via STDOUT. In a command-line environment, this means that errors are rendered in the terminal. In a web environment, errors and exceptions get displayed directly in the browser.

What is php stderr?

Technically, php://stderr refers to the STDERR stream for the process, but when running inside an Apache host as a module, this will be the STDERR of the Apache process. If it is a CLI or CGI app, this will not be the case.

What is Stdin PHP?

Standard streams php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.


2 Answers

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.

like image 140
Fabien Snauwaert Avatar answered Sep 19 '22 20:09

Fabien Snauwaert


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.

like image 24
Paulius Avatar answered Sep 18 '22 20:09

Paulius