[GH-ISSUE #798] Question: how to handle connection errors while logging? #305

Closed
opened 2026-03-04 02:13:59 +03:00 by kerem · 3 comments
Owner

Originally created by @doenietzomoeilijk on GitHub (May 27, 2016).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/798

I'm running into a problem, and I was wondering about the best way to tackle it.

I'm using Monolog to log to several handlers: a custom handler to write certain events to our own database, one to log important errors to Slack, and one that logs everything and the kitchen sink to Redis, to be picked up by an ELK stack. The last one is giving me a bit of grief, since every now and then, it can't connect to the Redis instance properly or it bombs out when writing a message. I was wondering what to do with the Exceptions that get thrown because of connection issues - I can and do catch them if they happen when using the initial connect, before I've pushed the Redishandler, but after that it's kinda out of my hands. If anything happens during storage (in our case: at the rpush stage), an exception gets thrown and bubbles all the way up into my application.

So I'm kind of torn here about what to do: my first hunch would be to try-catch pushing to the handlers and returning false if something is caught (so I don't have to change my code in all the places where I log things), but that'd make me lose sight of when logging failed to that handler. Alternatively I could wrap the Redis handler and have it log its problems in a separate logger (which would make the solution tightly bound to my app, and logging from within logging also sounds like something that could backfire at some point).

I was wondering about how other people would handle this issue. Again, the main solution is of course getting Redis to play nice, but at the same time I want to make my logging more robust / fault tolerant. As it stands, a faulty log receiver that is not critical to the application can bring the application down, and that shouldn't be possible IMO.

TL;DR misbehaving log receptacles are causing errors in my application; I'd prefer my application to keep working even if logging doesn't. What do?

Originally created by @doenietzomoeilijk on GitHub (May 27, 2016). Original GitHub issue: https://github.com/Seldaek/monolog/issues/798 I'm running into a problem, and I was wondering about the best way to tackle it. I'm using Monolog to log to several handlers: a custom handler to write certain events to our own database, one to log important errors to Slack, and one that logs everything and the kitchen sink to Redis, to be picked up by an ELK stack. The last one is giving me a bit of grief, since every now and then, it can't connect to the Redis instance properly or it bombs out when writing a message. I was wondering what to do with the Exceptions that get thrown because of connection issues - I can and do catch them if they happen when using the initial connect, before I've pushed the Redishandler, but after that it's kinda out of my hands. If anything happens during storage (in our case: at the `rpush` stage), an exception gets thrown and bubbles all the way up into my application. So I'm kind of torn here about what to do: my first hunch would be to try-catch pushing to the handlers and returning false if something is caught (so I don't have to change my code in all the places where I log things), but that'd make me lose sight of when logging failed to that handler. Alternatively I could wrap the Redis handler and have it log its problems in a separate logger (which would make the solution tightly bound to my app, and logging from within logging also sounds like something that could backfire at some point). I was wondering about how other people would handle this issue. Again, the main solution is of course getting Redis to play nice, but at the same time I want to make my logging more robust / fault tolerant. As it stands, a faulty log receiver that is not critical to the application can bring the application down, and that shouldn't be possible IMO. TL;DR misbehaving log receptacles are causing errors in my application; I'd prefer my application to keep working even if logging doesn't. What do?
kerem closed this issue 2026-03-04 02:13:59 +03:00
Author
Owner

@Seldaek commented on GitHub (May 27, 2016):

What do? Use WhatFailureGroupHandler to wrap redis and/or other handlers that you don't want to be able to blow up the app. By default we don't catch exception as it might surface a handler misconfiguration or such.

<!-- gh-comment-id:222101287 --> @Seldaek commented on GitHub (May 27, 2016): What do? Use [WhatFailureGroupHandler](https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/WhatFailureGroupHandler.php) to wrap redis and/or other handlers that you don't want to be able to blow up the app. By default we don't catch exception as it might surface a handler misconfiguration or such.
Author
Owner

@doenietzomoeilijk commented on GitHub (May 27, 2016):

That pretty much would be my solution, so thanks for pointing that one out as I had missed it myself. Too bad it's not usable right now, as I'm on PHP <7, but at least it tells my my idea wasn't totally insane. :) Thanks for the input!

<!-- gh-comment-id:222102942 --> @doenietzomoeilijk commented on GitHub (May 27, 2016): That pretty much would be my solution, so thanks for pointing that one out as I had missed it myself. Too bad it's not usable right now, as I'm on PHP <7, but at least it tells my my idea wasn't totally insane. :) Thanks for the input!
Author
Owner

@Seldaek commented on GitHub (May 27, 2016):

Oh no it is available in 1.x as well.

<!-- gh-comment-id:222104084 --> @Seldaek commented on GitHub (May 27, 2016): Oh no it is available in 1.x as well.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#305
No description provided.