Filtering by log level broken on 5.x branch

Created on 14 November 2023, 8 months ago

Problem/Motivation

I want to subscribe to the event so I can change the sentry client options. I want to send only the breadcrums of certain levels with the parameter 'before_breadcrumb'

Steps to reproduce

Create an event subscriber:

<?php

namespace Drupal\raven_sict\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Subscriber for Raven OptionsAlter event.
 */
class RavenOptionsAlterSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[\Drupal\raven\Event\OptionsAlter::class] = ['onOptionsAlter'];
    return $events;
  }

  /**
   * Alter options.
   *
   * @param \Drupal\raven\Event\OptionsAlter $alterEvent
   *   The Policy Alter event.
   */
  public function onOptionsAlter(\Drupal\raven\Event\OptionsAlter $alterEvent): void {
    // Disable Breadcrumb (Logging)
    // https://docs.sentry.io/platforms/php/enriching-events/breadcrumbs/#customize-breadcrumbs
    $alterEvent->options['before_breadcrumb'] = function (\Sentry\Breadcrumb $breadcrumb): ?\Sentry\Breadcrumb {
      if (in_array($breadcrumb->getLevel(), ['warning', 'error', 'fatal'])) {
        return $breadcrumb;
      }
      return null;
    };
  }

}

In https://sentry.io all levels are captured (info, debug, ...). Options changes don't seem to apply.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

5.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain carles.zapater

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

Comments & Activities

  • Issue created by @carles.zapater
  • πŸ‡ͺπŸ‡ΈSpain carles.zapater

    Tested in hook_cron()

    function raven_sict_cron() {
    \Drupal::logger('raven_sict')->notice('Notice');
    \Drupal::logger('raven_sict')->warning('Warning!');
    \Drupal::logger('raven_sict')->alert('Alert!');
    \Drupal::logger('raven_sict')->critical('Critical!');
    \Drupal::logger('raven_sict')->emergency('Emergency!');
    \Drupal::logger('raven_sict')->debug('Debug');
    \Drupal::logger('raven_sict')->error('Error!');
    \Drupal::logger('raven_sict')->info('Info');
    }

  • Status changed to Postponed: needs info 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Your code worked for me when I tested. Did you add it to your raven_sict.services.yml file?

    Should look like this:

      Drupal\raven_sict\EventSubscriber\RavenOptionsAlterSubscriber:
        tags:
          - { name: event_subscriber }
    
  • πŸ‡ͺπŸ‡ΈSpain carles.zapater

    Yes, I have added the service.

    The problem is that ignores the 'before_breadcrumb' option. However, it does work with the 'before_send' option.

    public function onOptionsAlter(\Drupal\raven\Event\OptionsAlter $alterEvent): void {
        // Disable Breadcrumb (Logging)
        // https://docs.sentry.io/platforms/php/enriching-events/breadcrumbs/#customize-breadcrumbs
        $alterEvent->options['before_send'] = function (\Sentry\Event $event): ?\Sentry\Event {
          if (in_array($event->getLevel(), ['warning', 'error', 'fatal'])) {
            return $event;
          }
          return null;
        };
      }
    

    I have also been able to observe that the 'log_levels' configuration of the configuration form is not used. Breadcrumbs of all levels are always created.

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

    Are you confusing events and breadcrumbs?

    The "log levels" configuration is for events. before_send is for events.

    There is no configuration for breadcrumbs (yet, although we could add it). before_breadcrumb is for breadcrumbs

  • πŸ‡ͺπŸ‡ΈSpain carles.zapater

    Sorry I want to filter log levels. If level checkboxes are selected in the configuration form, they are not filtered and all are passed.
    I have also been able to observe that the 'log_levels' configuration of the configuration form is not used.
    I have not seen in the source code that this setting is used.

  • Status changed to Active 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Ah, yes, there is a bug in 5.x branch, I accidentally removed that line of code while removing support for the old hooks. Thanks for reporting this

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
  • Status changed to Needs review 8 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    30 pass
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    28 pass, 1 fail
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Test-only patch (should fail)

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    30 pass
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
  • πŸ‡ͺπŸ‡ΈSpain carles.zapater

    Thanks! Could you check the 4.x branch too?

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

    This is a new issue because I changed that line of code when creating the 5.x branch.

    So I'm not going to check 4.x branch now, but if it turns out there's a problem please update this issue.

  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco
    • mfb β†’ committed ccfddffc on 5.x
      Issue #3401522 by mfb, carles.zapater: Filtering by log level broken on...
  • Status changed to Fixed 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Fixed in 5.0.3 release.

Production build 0.69.0 2024