沃梦达 / 编程问答 / php问题 / 正文

Symfony 记录到 Docker 容器内的标准输出

Symfony logs to stdout inside Docker container(Symfony 记录到 Docker 容器内的标准输出)

本文介绍了Symfony 记录到 Docker 容器内的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Symfony 应用程序构建一个 docker 映像.在这张图片中,我想将 Symfony 日志流式传输到标准输出.因此,与配置 nginx 日志的方式类似,我在 Dockerfile 中添加了这一行:

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:

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 致命错误:未捕获的异常 '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): 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(对象(SymfonyComponentDebugExceptionFatalErrorException), Array)
#6 [内部函数]: SymfonyComponentDebugE in/var/www/project/app/cache/prod/classes.php on line 5808

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

有什么建议吗?

推荐答案

借助Monolog,很容易将日志发送到stdout/stderr.我的例子是使用stderr,但我认为stdout是一样的.

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

path:  "php://stderr"

但是你还没有完成.您还必须相应地配置 PHP.工作人员必须捕获其进程的输出并将此输出再次记录到他们的标准错误中.

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 配置

#/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 配置

# 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

如果您在胖 docker 容器中使用任何过程控制系统,则必须确保该系统也记录到 stdout(或 stderr).

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).

以主管为例:

[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

总而言之,请确保:

  • 应用程序记录到 stdout/stderr
  • PHP 捕获 worker 输出并记录到 stderr
  • 可选:任何流程控制系统都必须将托管流程的输出转发到 stdout/stderr

这篇关于Symfony 记录到 Docker 容器内的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Symfony 记录到 Docker 容器内的标准输出

基础教程推荐