在gelf连接失败时使用Symfony 2/Monolog防止内部服务器错误 [英] Prevent Internal Server Error with Symfony 2 / Monolog on failed gelf connection

查看:42
本文介绍了在gelf连接失败时使用Symfony 2/Monolog防止内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 gelf 格式将日志从 symfony 2 应用程序流式传输到 graylog 2 服务器.

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

我的 Monolog 配置如下:

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

当 Graylog 服务器不可用时,我收到(可以理解)连接被拒绝错误

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) [] []

导致内部服务器错误(对导致日志的请求的 500 响应代码).

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

捕获此错误的最佳方法是什么?有错误日志是合理的,但我不希望我的请求在外部日志处理服务器不可用时出错?

What's the best way to catch this error? Having an error log is resonable, but I don't want my requests to error when the external log processing server is not available?

推荐答案

最好的办法是在你的应用服务器上有一个本地缓存机制来转发请求.这样,如果日志服务器暂时停机,请求可以排队.像 fluentd 这样的系统可以很好地解决这个问题.另一方面,如果您不关心丢失日志消息,则可以查看忽略错误的传输,例如 IgnoreErrorTransportWrapper:

Best bet is to have a local caching mechanism on your application server to forward requests. That way if there is momentary downtime of the log server, the requests can be queued. Systems like fluentd work well for this. On the other hand, if you don't care about losing log messages, you can look into a Transport that ignores errors such as IgnoreErrorTransportWrapper:

https://github.com/bzikarsky/gelf-php/blob/master/src/Gelf/Transport/IgnoreErrorTransportWrapper.php

进一步阅读:

https://github.com/bzikarsky/gelf-php/issues/56

这篇关于在gelf连接失败时使用Symfony 2/Monolog防止内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆