doujiao3074 2017-08-02 08:17
浏览 75
已采纳

Graylog / Symfony2 / Gelf:如何配置记录器以忽略错误?

I'm trying to stream logs from a symfony 2 app to a graylog 2 server using the gelf format.

My monolog configuration looks as follows:

monolog:
    handlers:
          # --- 8< ---
          # ...
          # --- >8 --- 
          graylog:
              type: gelf
              publisher:
                  hostname: my-graylog-server.com
                  port: 12201
              level: debug
              formatter: app.gelf_formatter

When the graylog server is not available, I get (understandably) a connection refused error

[2017-07-28 16:03:25] app.ERROR: Failed to write to socket: fwrite(): send of 153 bytes failed with errno=111 Connection refused (8) [] []

which results in an internal server error (500 response code to the request causing the log).

(See also this question: Prevent Internal Server Error with Symfony 2 / Monolog on failed gelf connection)

gelf-php provides an IngoreErrorTransportLogger, which seems to be build for this exact purpose.

How can I configure this in Symfony's monolog config?

  • 写回答

1条回答 默认 最新

  • doukao2180 2017-08-02 09:12
    关注

    It turns out that Symfony has WhatFailureGroup handler wrapper a handler which ignores errors from the logs of all logging handlers that it wraps.

    Our config now looks like this, it just drops errors during logging (probably not ideal, but better than failing outright due to an error during logging).

    monolog:
        handlers:
              main:
                  type:         fingers_crossed
                  action_level: warning
                  handler:      grouped
              grouped:
                  type:         whatfailuregroup
                  members:      [file, graylog]
              file:
                  type:         stream
                  path:         "%kernel.logs_dir%/%kernel.environment%.log"
                  level:        debug
              graylog:
                  type: gelf
                  publisher:
                      hostname: my-graylog-server.com
                      port: 12201
                  level: debug
                  formatter: app.gelf_formatter
    

    Essentially we replaced type: group with type: whatfailuregroup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口