1

I'm speaking coming from a PHP background, but I think this applies to other web-server languages. Isn't stdout where all, as named, standard output goes? Also, isn't that where the HTTP response goes? Am I missing something?

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    Run a command line application which is a web server… you'll see some debug output on the command line (*stdout*), but the incoming network connections it's handling are an entirely separate thing from that. – deceze Aug 24 '17 at 15:15

2 Answers2

5

Traditionally CGI applications used STDOUT to send HTTP responses. CGI application are just some executable, that's called by the webserver. CGI application use STDIN to read requests, and STDOUT to emit responses. What's great about CGI applications, is that they don't need to understand the network. They fit brilliantly in the UNIX model.

PHP started as a CGI application. CGI applications aren't really popular anymore (they don't scale well), but PHP does still follow this basic model. So you're not really able to log events to STDOUT in PHP, because PHP is not the webserver. STDOUT in PHP is indeed the HTTP response.

Your entire PHP application starts up, bootstraps and dies all within the context of every single HTTP request.

An web application that starts its own TCP daemon and has its own HTTP server can do this. It will have a STDOUT, and it will have a TCP connection for every client to write HTTP responses to. Some examples are Go, Node.js, Java but this applies to any other language that doesn't follow the CGI model.

So this isn't really something that makes sense for PHP. It can make sense if you use PHP to open a TCP server socket and implement the HTTP protocol in PHP. Some people do this (see reactphp, amphp) but you probably shouldn't do this unless you have a really good reason to. Being able to use STDOUT for logging is not one of those reasons. syslog is fine.

Evert
  • 93,428
  • 18
  • 118
  • 189
0

After reading Evert's answer, I think I've cleared up some long held assumptions, so I did an experiment.

<?php
echo "Echo output\n";
fwrite(STDOUT, "stdout output\n");
?>
Post-closing tag output

Running this on a command line shows this:

Echo output
stdout output
Post-closing tag output

But viewing this file through a browser shows this:

Echo output
Post-closing tag output

Meaning, depending on the context where PHP is run, its output is directed to different places. If served through a web-server, echo and anything outside of the PHP tags go to the HTTP response (at least on Apache2/PHP5). This HTTP response is different from stdout. If ran via CLI echo goes to stdout.

This question may also be relevant: PHP stdout on Apache