我有一个码头工人在AWS上托管的应用Symfony的。为了记录我使用AWS CloudWatch的。
例如:数据库连接不能建立。如果我使用默认的独白配置,我得到的文件/project/var/log/prod.log错误。当我改变路径到PHP://标准错误,我希望在CloudWatch的相同的消息,但它不会出现。
我已经修改了测试的原因的index.php:
<?php
echo ini_get('error_log');
error_log('error_log');
file_put_contents('php://stderr', 'file_put_contents', FILE_APPEND);
输出是:/proc/self/fd/2
(搬运工日志目标)
在我的CloudWatch得到消息error_log
,但不file_put_contents
。
我不知道问题出在哪里。也许Symfony的应用程序配置错误。但由于消息file_put_contents
丢失 - 这运行没有Symfony的 - 我不知道。
这是独白配置:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
nested:
type: stream
# path: "%kernel.logs_dir%/%kernel.environment%.log"
path: "php://stderr"
level: debug
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
deprecation:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.deprecations.log"
deprecation_filter:
type: filter
handler: deprecation
max_level: info
channels: ["php"]
这是默认的配置,除了线path: "php://stderr"
。
我设法通过以下获得的Symfony记录到的CloudWatch:
使用这个包:
composer require maxbanton/cwh:^1.0
然后在我的config/packages/prod/monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
grouped:
type: group
members: [cloudwatch, nested]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
cloudwatch:
type: service
id: cloudwatch_handler
level: error
然后在我的config/services.yaml
services:
cloudwatch_client:
class: Aws\CloudWatchLogs\CloudWatchLogsClient
arguments:
- credentials: { key: '%env(AWS_KEY)%', secret: '%env(AWS_SECRET)%' }
region: '%env(AWS_REGION)%'
version: "2014-03-28"
cloudwatch_handler:
class: Maxbanton\Cwh\Handler\CloudWatch
arguments:
- "@cloudwatch_client"
- "symfony" # groupName
- "%kernel.environment%" # streamName
- 30 # retentionDays
- 10000 # logsInBatch
- { mytag: "symfony" } # tags
- WARNING # logLevel
然后,我必须确保我所立的API密钥已经为CloudWatch的权限IAM但之后,它完美记录