Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony logging with Monolog, confused about STDERR

I am trying to align my logging with the best practice of using STDERR.

So, I would like to understand what happens with the logs sent to STDERR.

Symfony official docs (https://symfony.com/doc/current/logging.html):

In the prod environment, logs are written to STDERR PHP stream, which works best in modern containerized applications deployed to servers without disk write permissions.

If you prefer to store production logs in a file, set the path of your log handler(s) to the path of the file to use (e.g. var/log/prod.log).

This time I want to follow the STDERR stream option. When I was writing to a specific file, I knew exactly where to look for that file, open it and check the logged messages. But with STDERR, I don't know where to look for my logs.

So, using monolog, I have the configuration:

monolog:
    handlers:
        main:
            type: fingers_crossed
            action_level: error
            handler: nested
            excluded_http_codes: [404, 405]
        nested:
            type: stream
            path: "php://stderr"
            level: debug

Suppose next morning I want to check the logs. Where would I look?

like image 587
user13859151 Avatar asked Oct 29 '25 04:10

user13859151


1 Answers

Several hours of reading docs later, my understanding is as follows:

First, the usage of STDERR over STDOUT is preferred for errors because it is not buffered (gathering all output waiting for the script to end), thus errors are thrown immediately to the STDERR stream. Also, this way the normal output doesn't get mixed with errors.

Secondly, the immediate intuitive usage is when running a shell script, because in the Terminal one will directly see the STDOUT and STDERR messages (by default, both streams output to the screen).

But then, the non-intuitive usage of STDERR is when logging a website/API. We want to log the errors, and we want to be able to monitor the already occurred errors, that is to come back later and check those errors. Traditional practice stores errors in custom defined log-files. More modern practice recommends sending errors to STDERR. Regarding Symfony, Fabien Potencier (the creator of Symfony), says:

in production, stderr is a better option, especially when using Docker, SymfonyCloud, lambdas, ... So, I now recommend to use php://stderr (https://symfony.com/blog/logging-in-symfony-and-the-cloud).

And he further recommends using STDERR even for development.

Now, what I believe to be missing from the picture (at least for me, as non-expert), is the guidance on HOW to access and check the error logs. Okay, we send the errors to STDERR, and then? Where am I going to check the errors next morning? I get it that containerized platforms (clouds, docker etc) have specific tools to easily and nicely monitor logs (tools that intercept STDERR and parse the messages in order to organize them in specific files/DBs), but that's not the case on a simple server, be it a local server or on a hosting.

Therefore, my understanding is that sending errors to STDERR is a good standardization when:

  1. Resorting to using a third-party tool for log monitoring (like ELK, Grail, Sentry, Rollbar etc.)

  2. When knowing exactly where your web-server is storing the STDERR logs. For instance, if you try (I defined a new STD_ERR constant to avoid any pre-configs):

define('STD_ERR', fopen('php://stderr', 'wb'));
fputs(STD_ERR, "ABC error message.");

you can find the "ABC error message" at:

XAMPP Apache default (Windows):

..\xampp\apache\logs\error.log

Symfony5 server (Windows):

C:\Users\your_user\.symfony5\log\ [in the most recent folder, as the logs rotate]

Symfony server (Linux):

/home/your_user/.symfony/log/ [in the most recent folder, as the logs rotate]

For Symfony server, you can actually see the logs paths when starting the server, or by command "symfony server:log".

One immediate advantage is that these STDERR logs are stored outside of the app folders, and you do not need to maintain extra writable folders or deal with the permissions etc. Of course, when developing/hosting multiple sites/apps, you need to configure the error log (the STDERR storage) location per app (in Apache that would be inside each <VirtualHost> conf ; with Symfony server, I am not sure). Personally, without a third-party tool for monitoring logs, I would stick with custom defined log files (no STDERR), but feel free to contradict me.

like image 58
user13859151 Avatar answered Nov 01 '25 12:11

user13859151



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!