我正在为Symfony应用程序构建一个docker镜像。在此图像中,我想将Symfony日志流式传输到stdout。因此,类似于如何配置nginx日志,我将此行添加到我的Dockerfile:
ln -sf /dev/stdout /var/www/project/app/logs/prod.log
在容器内部,我可以看到:
$ 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
然而,该应用程序抛出以下错误:
PHP致命错误:未捕获异常'UnexpectedValueException',消息'流或文件“/var/www/project/app/logs/prod.log”无法打开:无法打开流:没有此类文件或目录'in /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 (数组)
#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','致命错误:Un ......',数组)
#5 / var / www / project / app / cache / prod / classes.php(2034):Symfony \ Component \ Debug \ ErrorHandler-> handleException(Object(Symfony \ Component \ Deb) ug \ Exception \ FatalErrorException),Array)
#6 [内部函数]:第5808行/var/www/project/app/cache/prod/classes.php中的Symfony \ Component \ Debug \ E
有什么建议吗?
答案 0 :(得分:33)
在Monolog的帮助下,将日志发送到stdout / stderr非常容易。我的例子是使用stderr,但我认为它与stdout相同。
您只需输入首选的流路径
,而不是定义日志文件path: "php://stderr"
但你还没有完成。您还必须相应地配置PHP。工作人员必须捕获其进程的输出并将此输出再次记录到其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)。
主管的例子:
[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
总而言之:
答案 1 :(得分:-1)
我的配置将所有php信息/警告/错误发送到docker日志(我正在使用php:7.1-apache映像):
php.ini
log_errors = On
error_reporting = E_ALL | E_STRICT
error_log = /dev/stderr
Symfony配置
# app/config/config_prod.yml
monolog:
handlers:
main:
type: group
members: [logfile, dockerlog]
logfile:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ['!event', '!doctrine']
dockerlog:
type: stream
path: "php://stderr"
level: debug
channels: ['!event', '!doctrine']
故障排除: