【问题标题】:Symfony logger failing to write in a log fileSymfony 记录器无法写入日志文件
【发布时间】:2021-08-03 09:31:45
【问题描述】:

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

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

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

所以我安装了monolog并实现了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' ]

我在api/var/log/ 中创建了一个twilio.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 文件是空的,我不明白为什么记录器没有写入它

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

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

谢谢!

【问题讨论】:

    标签: symfony logging callback twilio monolog


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2019-09-27
      • 2017-11-09
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多