Pass criticality/fatality of log events to loggers

Created on 10 February 2023, over 1 year ago
Updated 4 May 2024, 14 days ago

Problem/Motivation

ExceptionLoggingSubscriber::onError() has a concept of $is_critical errors and _drupal_log_error() has a concept of $fatal errors, which these methods use to decide if an error/exception is fatal/critical enough to be logged to the server log.

Any other logger may want to know the same thing, but this criticality/fatality is not passed down to loggers. Likewise, loggers may want to know if an error/exception was merely caught, logged and ignored, or was not otherwise handled and is being logged by one of the final error/exception logging mechanisms.

I'd propose adding something to the context array so loggers can mark an error as critical/fatal/unhandled/etc., send out an alert, or whatever the logging system wants to do with that metadata.

Steps to reproduce

Implement a logger and try to figure out if a log event should be considered critical/fatal or not. (Are these the same thing? I think basically so, except that the event arrived thru a different mechanism :)

This requires replicating the $is_critical logic in ExceptionLoggingSubscriber::onError(), and using debug_backtrace() to check the $fatal argument in _drupal_log_error().

Proposed resolution

This concept of criticality/fatality is related to the severity level, but that's already determined elsewhere, so seems best to add some (presumably namespaced?) context element(s) to indicate criticality/fatality. Current proposal is to provide a "mechanism" key indicating the function or class doing the logging, "is_fatal" key indicating fatal errors and "is_critical" key for critical exceptions.

Remaining tasks

Review concept/approach; add tests

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Needs review

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • Issue created by @mfb
  • Pipeline finished with Success
    16 days ago
    Total: 565s
    #162907
  • Status changed to Needs review 16 days ago
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
  • πŸ‡ΊπŸ‡ΈUnited States cmlara

    Why add it as an array pass through?” (more array mess)

    β€œImplement a logger and try to figure out if a log event should be considered critical/fatal or not.”
    I would be curious if you have a real life scenario that this information is actually useful for?

    To me it appears this is internal method data that is not needed or useful outside the method. Primarily looks like it is intended to control if an error gets raised to the server logs as well as the Drupal logs.

    If it is important why not add it directly to the logged message rather than just hoping for a logger to act on it?

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    @cmlara the PSR-3 logging standard provides a context array to pass arbitrary context data to loggers. So that's what I'm proposing to use (it may be helpful to somehow namespace usage of the context array, however, as I mentioned in the issue summary)

    In Raven module β†’ we try to figure out if an error/exception was more-or-less "handled" somehow (i.e. caught, logged and ignored), or more-or-less "unhandled" and logged by one of the final error/exception logging mechanisms.

    At the moment, core doesn't add anything to the context array about the disposition of the error or the mechanism logging it, so the best a logger can do is try to figure that out by looking at the backtrace. Which works fine, but is a bit hacky for my taste.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Updated the issue summary to explain why this internal method data may be useful to other loggers, not just Drupal core.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
Production build 0.67.2 2024