【问题标题】:Configure Monolog to send GELF via TCP in Symfony 4在 Symfony 4 中配置 Monolog 通过 TCP 发送 GELF
【发布时间】:2019-06-24 14:38:38
【问题描述】:

我的 monolog.yaml 文件中有以下配置:

monolog:
    handlers:
        main:
            type: stream
            path: "php://stdout"
            level: debug
            channels: ["!event"]
        gelf:
            type: gelf
            publisher:
                hostname: mygelfhost.com
                port: 12201
        console:
            type: console
            process_psr_3_messages: false
            channels: ["!event", "!doctrine", "!console"]

但它只使用 UDP 向 mygelfhost.com 发送消息。当我试图把:

hostname: tcp://mygelfhost.com

我收到以下错误:

  Failed to create socket-client for udp://tcp://log-dev.hpa.lis-dev.net:12201: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)  

我的目标是通过 TCP 将日志发送到同一主机,我在这里检查了配置:https://github.com/symfony/monolog-bundle/blob/master/DependencyInjection/Configuration.php#L25 没有可能的解决方案。

【问题讨论】:

    标签: php symfony graylog gelf


    【解决方案1】:

    您需要为 Gelf 指定传输。默认情况下它使用 UDP。

    monolog:
        handlers:
            gelf:
                type: gelf
                id: my_tcp_gelf_publisher 
    

    在您的服务中:

    services:
        my_tcp_gelf_publisher:
            class: Gelf\Publisher
            arguments: [@gelf.tcp_transport]
        gelf.tcp_transport:
            class: Gelf\Transport\TcpTransport
            arguments:
                - "mygelfhost.com" # Hostname
                - 12201 #port                  
    

    这样的东西应该可以工作,不过我还没有测试过代码。基于定义发布者和来自的配置示例:https://github.com/bzikarsky/gelf-php

    【讨论】:

      【解决方案2】:

      基于@Chase 的回答:

      monolog:
          handlers:
              gelf:
                  type: gelf
                  publisher: my_tcp_gelf_publisher 
      

      上面的区别是 my_tcp_gelf_publisher 是 Publisher 对象,而不是 Handler 对象。您想告诉处理程序,哪个发布者使用“my_tcp_gelf_publisher”,因为这是在 services.yaml 中定义的

      在 services.yaml 中:

      services:
          my_tcp_gelf_publisher:
              class: Gelf\Publisher
              arguments: ["@gelf.tcp_transport"]
          gelf.tcp_transport:
              class: Gelf\Transport\TcpTransport
              arguments:
                  - "mygelfhost.com" # Hostname
                  - 12201 #port  
      

      这几乎相同,只是您需要将 @gelf.tcp_transport 服务引用放在引号中,否则会出现缩放错误。

      额外提示,如果您偶然决定将传输配置为使用端口 12202(Gelf\Transport\TcpTransport 的 SSL 端口),请确保您的端点支持 ssl 并提供正确配置的 SslOptions 对象,否则使用标准端口12201(或任何其他端口)。

      【讨论】:

        猜你喜欢
        • 2020-10-14
        • 1970-01-01
        • 1970-01-01
        • 2015-03-24
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 2016-08-21
        • 2011-12-06
        相关资源
        最近更新 更多