问题
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).
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?
回答1:
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
Further reading:
https://github.com/bzikarsky/gelf-php/issues/56
来源:https://stackoverflow.com/questions/45376128/prevent-internal-server-error-with-symfony-2-monolog-on-failed-gelf-connection