首页 > 解决方案 > Symfony 记录器无法写入日志文件

问题描述

我正在使用带有 API 平台、docker-compose 和 Twilio 的 Symfony 5,并且正在使用 SMS 消息传递功能。

因此,在我的 Twilio 控制台中,在 SMS 回调中,我输入了使用 ngrok 打开的路由:https://f64560c9efff.ngrok.io/twilio/intervention/request/response

要查看 Twilio 响应的内容,我想实现一个记录器

所以我安装了独白并实现了conf文件packages/dev/monolog.yaml

monolog:
    channels: ['twilio']
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: ["!event"]
        # uncomment to get logging in your browser
        # you may have to allow bigger header sizes in your Web server configuration
        #firephp:
        #    type: firephp
        #    level: info
        #chromephp:
        #    type: chromephp
        #    level: info
        console:
            type: console
            process_psr_3_messages: false
            channels: ["!event", "!doctrine", "!console"]
        twilio:
            type: stream
            path: '%kernel.logs_dir%/twilio.log'
            channels: [ 'twilio' ]

我已经创建了一个twilio.log文件api/var/log/chmod 777测试

在我的控制器中,我正在做:

    private $em;
    private $twilioLogger;

    public function __construct(EntityManagerInterface $em,LoggerInterface $twilioLogger)
    {
        $this->em = $em;
        $this->twilioLogger = $twilioLogger;
    }

    /**
     * @Route("/twilio/route", name="twilio_route")
     */
    public function twilioRoute(Request $request): Response
    {
        $answer = new Answer();
        $answer->setResponse($request->getContent());
        $answer->setResponseAt(new DateTimeImmutable());

        $this->twilioLogger->info("WE PASS HERE");
        $this->twilioLogger->info($request->getContent());

        $this->em->persist($answer);
        $this->em->flush();

        return new Response(Response::HTTP_OK);
    }

所以我知道 Twilio 的回调通过了导致Answer只有该responseAt字段的对象已被持久化

但是我的twilio.log文件是空的,我不明白为什么记录器没有写入

我错过了一个配置还是我错过了什么?

如果有人有想法或线索,我将不胜感激!

谢谢!

标签: symfonyloggingcallbacktwiliomonolog

解决方案


我没有做任何与你不同的事情。然而,有一件事确实很突出:

我在 api/var/log/ 中创建了一个 twilio.log 文件

默认情况下,日志文件是在var/lognot in中创建的api/var/log。当我模拟一个简单的示例时,该文件是为我创建的并包含相关的日志消息。我希望这个对你有用。


推荐阅读