简体   繁体   English

带有CloudWatch的AWS上Symfony应用中的PHP日志目标

[英]PHP log target in Symfony app on AWS with CloudWatch

I have a Symfony app hosted with Docker on AWS. 我有一个AWS托管在Docker上的Symfony应用程序。 For logging I use AWS CloudWatch. 对于日志记录,我使用AWS CloudWatch。

For example: the database connection can not be established. 例如:无法建立数据库连接。 If I use the default monolog configuration, I get the error in the file /project/var/log/prod.log . 如果使用默认的独白配置, 则会在文件/project/var/log/prod.log中得到错误。 When I change the path to php://stderr , I expect the same message in CloudWatch, but it doesn't appear . 当我将路径更改为php:// stderr时我希望CloudWatch中出现相同的消息,但不会出现

I have modified the index.php for test reasons: 我出于测试原因修改了index.php:

<?php
echo ini_get('error_log');
error_log('error_log');
file_put_contents('php://stderr', 'file_put_contents', FILE_APPEND);

The output is: /proc/self/fd/2 (docker logs target) 输出为: /proc/self/fd/2 (docker日志目标)

In CloudWatch I get the message error_log , but not file_put_contents . 在CloudWatch中,我收到消息error_log ,但没有file_put_contents消息。

I have no idea where the problem is. 我不知道问题出在哪里。 Maybe the Symfony app is misconfigured. 也许Symfony应用配置错误。 But since the message file_put_contents is missing - which runs without Symfony - I'm not sure. 但是由于缺少消息file_put_contents它在没有Symfony的情况下运行-我不确定。

This is the monolog configuration: 这是独白配置:

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"]

This is the default configuration, except the line path: "php://stderr" . 这是默认配置,除了以下行path: "php://stderr"

I managed to get Symfony logging to CloudWatch by the following: 我设法通过以下方法将Symfony日志记录到CloudWatch:

Use this package: 使用此软件包:

composer require maxbanton/cwh:^1.0

Then in my config/packages/prod/monolog.yaml 然后在我的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

Then in my config/services.yaml 然后在我的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

I then had to make sure the API Key I had set up had permissions for CloudWatch in IAM but after that it logged perfectly 然后,我必须确保已设置的API密钥在IAM中具有CloudWatch的权限,但此后它可以完美记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM