Skip to content
Advertisement

Symfony logs to stdout inside Docker container

I’m building a docker image for a Symfony application. In this image, I want to stream the Symfony logs to stdout. So, similar to how nginx logs are configured, I added this line to my Dockerfile:

JavaScript

Inside the container, I can see this:

JavaScript

However, the app throws following error:

PHP Fatal error: Uncaught exception ‘UnexpectedValueException’ with message ‘The stream or file “/var/www/project/app/logs/prod.log” could not be opened: failed to open stream: No such file or directory’ in /var/www/project/app/cache/prod/classes.php:5808
Stack trace:
#0 /var/www/project/app/cache/prod/classes.php(5746): MonologHandlerStreamHandler->write(Array)
#1 /var/www/project/app/cache/prod/classes.php(5917): MonologHandlerAbstractProcessingHandler->handle(Array)
#2 /var/www/project/app/cache/prod/classes.php(6207): MonologHandlerFingersCrossedHandler->handle(Array)
#3 /var/www/project/app/cache/prod/classes.php(6276): MonologLogger->addRecord(500, ‘Fatal Error: Un…’, Array)
#4 /var/www/project/app/cache/prod/classes.php(1978): MonologLogger->log(‘critical’, ‘Fatal Error: Un…’, Array)
#5 /var/www/project/app/cache/prod/classes.php(2034): SymfonyComponentDebugErrorHandler->handleException(Object(SymfonyComponentDebugExceptionFatalErrorException), Array)
#6 [internal function]: SymfonyComponentDebugE in /var/www/project/app/cache/prod/classes.php on line 5808

Any suggestions ?

Advertisement

Answer

With the help of Monolog, it is very easy to send logs to stdout/stderr. My examples are using stderr, but I think it’s the same with stdout.

Instead of defining a log file you just enter the preferred stream path

JavaScript

BUT you are not done yet. You also have to configure PHP accordingly. The workers have to catch the output of their processes and log this output again to their stderr.

PHP Configuration

JavaScript

Symfony Configuration

JavaScript

If you are using any process control system in a fat docker container you have to make sure that this system also logs to stdout (or stderr).

Example with supervisor:

JavaScript

All in all make sure that:

  • The application logs to stdout/stderr
  • PHP catches workers output and logs to stderr
  • optional: any process control system has to forward output of managed processes to stdout/stderr
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement