Exception thrown in a handler prevents other handlers to be called

Created on 17 January 2023, over 1 year ago
Updated 2 April 2023, about 1 year ago

Problem/Motivation

We have configured multiple handlers to send logs to multiple log collectors. We realized that if the one of the handlers fails with an exception (like a SocketHandler "Could not write a socket", ) then the rest of the handlers (fallbacks) are not going to be called.

Steps to reproduce

Configure multiple handler for a channel and trigger an exception in the first handler.

Proposed resolution

Monolog has a WhatFailureGroupHandler handler that could be used in \Drupal\monolog\Logger\MonologLoggerChannelFactory::getLogger() to create a handler stack that tolerates failures and ensures that log distribution is not aborted.

Alternative considers, maybe in a follow up task...
The \Drupal\monolog\Logger\MonologLoggerChannelFactory::getLogger() might also provide support for configuring an exception handler for the logger that could take care of handling when a processor or a handler failed with an exception.

Remaining tasks

* Backport fix to 2.x branch.

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

3.0

Component

Code

Created by

🇭🇺Hungary mxr576 Hungary

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Needs review over 1 year ago
  • 🇭🇺Hungary mxr576 Hungary

    @lussoluca, ping, kind reminder about this issue

  • 🇮🇹Italy lussoluca Italy

    Sorry for the late response...

    I'm checking the WhatFailureGroupHandler, but it seems to me that it ignores the bubbling of each handlers added to it, or I'm wrong?

    If some of the handlers added to the group stops the bubbling, all other handlers are executed anyway. This may be OK, but it's a change in the actual behaviour. Maybe we can made this configurable? Allow a developer to choose if a set of handlers must be wrapped by a WhatFailureGroupHandler

  • Status changed to Needs work about 1 year ago
  • 🇮🇹Italy lussoluca Italy

    Something like:

      monolog.channel_handlers:
        default:
          handlers:
            - name: 'handler_that_may_fail'
              formatter: 'json'
            - name: 'fallback_handler'
              formatter: 'json'
            - name: 'handler_that_will_always_work'
              formatter: 'json'
      monolog.fallback_group:
        - handler_that_may_fail
        - fallback_handler
  • 🇮🇹Italy lussoluca Italy

    or:

      monolog.channel_handlers:
        default:
          handlers:
            - name: 'handler_that_may_fail'
              formatter: 'json'
              group: 'group_1'
            - name: 'fallback_handler'
              formatter: 'json'
              group: 'group_1'
            - name: 'handler_that_will_always_work'
              formatter: 'json'
     

    If "group" is missing, we add the handler to a default group.

Production build 0.69.0 2024