Symfony将日志记录到Docker容器内的stdout [英] Symfony logs to stdout inside Docker container

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

问题描述

我正在为Symfony应用程序构建一个docker映像.在此图像中,我想将Symfony日志流式传输到stdout.因此,类似于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致命错误:无法打开消息流或文件"/var/www/project/app/logs/prod.log"的未捕获异常'UnexpectedValueException':无法打开流:没有此类文件或目录'在/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 \ FatalErrorEx数组)
#6 [内部功能]:第5808行的/var/www/project/app/cache/prod/classes.php中的Symfony \ Component \ Debug \ E

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

有什么建议吗?

推荐答案

借助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.工人必须捕获其过程的输出,并将该输出再次记录到其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配置

#/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捕获工作人员的输出并记录到stderr
  • 可选:任何过程控制系统都必须将托管过程的输出转发到stdout/stderr

这篇关于Symfony将日志记录到Docker容器内的stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆