php Symfony 将日志记录到 Docker 容器内的标准输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38499825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony logs to stdout inside Docker container
提问by Webberig
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:
我正在为 Symfony 应用程序构建一个 docker 镜像。在这张图片中,我想将 Symfony 日志流式传输到标准输出。因此,类似于 nginx 日志的配置方式,我将这一行添加到我的 Dockerfile 中:
ln -sf /dev/stdout /var/www/project/app/logs/prod.log
Inside the container, I can see this:
在容器内,我可以看到:
$ ls /var/www/project/app/logs/ -l
total 12
-rw-r--r-- 1 501 games 4473 Jul 21 08:36 dev.log
lrwxrwxrwx 1 501 games 11 Jul 21 08:35 prod.log -> /dev/stdout
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): Monolog\Handler\StreamHandler->write(Array)
#1 /var/www/project/app/cache/prod/classes.php(5917): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#2 /var/www/project/app/cache/prod/classes.php(6207): Monolog\Handler\FingersCrossedHandler->handle(Array)
#3 /var/www/project/app/cache/prod/classes.php(6276): Monolog\Logger->addRecord(500, 'Fatal Error: Un...', Array)
#4 /var/www/project/app/cache/prod/classes.php(1978): Monolog\Logger->log('critical', 'Fatal Error: Un...', Array)
#5 /var/www/project/app/cache/prod/classes.php(2034): Symfony\Component\Debug\ErrorHandler->handleException(Object(Symfony\Component\Debug\Exception\FatalErrorException), Array)
#6 [internal function]: Symfony\Component\Debug\E in /var/www/project/app/cache/prod/classes.php on line 5808
PHP 致命错误:未捕获的异常“UnexpectedValueException”,消息为“无法打开流或文件“/var/www/project/app/logs/prod.log”:无法打开流:在 / 中没有这样的文件或目录var/www/project/app/cache/prod/classes.php:5808
堆栈跟踪:
#0 /var/www/project/app/cache/prod/classes.php(5746): Monolog\Handler\StreamHandler->write (Array)
#1 /var/www/project/app/cache/prod/classes.php(5917): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#2 /var/www/project/app/cache/prod /classes.php(6207): Monolog\Handler\FingersCrossedHandler->handle(Array)
#3 /var/www/project/app/cache/prod/classes.php(6276): Monolog\Logger->addRecord(500, '致命错误:Un...',数组)
#4 /var/www/project/app/cache/prod/classes.php(1978): Monolog\Logger->log('critical', 'Fatal Error: Un...', Array)
#5 /var/ www/project/app/cache/prod/classes.php(2034): Symfony\Component\Debug\ErrorHandler->handleException(Object(Symfony\Component\Debug\Exception\FatalErrorException), Array)
#6 [内部函数]: /var/www/project/app/cache/prod/classes.php 中的 Symfony\Component\Debug\E 行 5808
Any suggestions ?
有什么建议 ?
回答by frastel
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.
在 Monolog 的帮助下,可以很容易地将日志发送到 stdout/stderr。我的示例使用的是 stderr,但我认为它与 stdout 相同。
Instead of defining a log file you just enter the preferred stream path
您只需输入首选的流路径,而不是定义日志文件
path: "php://stderr"
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。工作人员必须捕获其进程的输出并将此输出再次记录到他们的 stderr。
PHP Configuration
PHP 配置
#/etc/php/7.0/fpm/php-fpm.conf
error_log = /proc/self/fd/2
#/etc/php/7.0/fpm/pool.d/www.conf
catch_workers_output = yes
Symfony Configuration
Symfony 配置
# app/config/config_prod.yml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "php://stderr"
level: debug
console:
type: console
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).
如果您在胖 docker 容器中使用任何过程控制系统,您必须确保该系统也记录到 stdout(或 stderr)。
Example with supervisor:
与主管的示例:
[supervisord]
nodaemon=true
;@see http://blog.turret.io/basic-supervisor-logging-with-docker/
;we need the output from the controlled processes
;but this is only possible with lowered loglevel
loglevel=debug
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
- 应用程序登录到 stdout/stderr
- PHP 捕获 worker 输出并记录到 stderr
- 可选:任何进程控制系统都必须将托管进程的输出转发到 stdout/stderr