Skip to content

Conversation

HypeMC
Copy link
Member

@HypeMC HypeMC commented Aug 9, 2024

Adds support for getsentry/sentry-php#1199.

monolog:
  handlers:
    sentry:
      type: sentry
      hub_id: Sentry\State\HubInterface
      level: !php/const Monolog\Logger::ERROR
    sentry_breadcrumb:
      type: sentry_breadcrumb
      sentry_handler: sentry
      level: !php/const Monolog\Logger::INFO

# or if you don't have Sentry bundle installed

monolog:
  handlers:
    sentry:
      type: sentry
      dsn: "https://public@sentry.example.com/1"
      level: !php/const Monolog\Logger::ERROR
    sentry_breadcrumb:
      type: sentry_breadcrumb
      sentry_handler: sentry
      level: !php/const Monolog\Logger::INFO

Initially, I considered using the existing handler option instead of introducing the new sentry_handler option. However, I decided against it because the PSR logger doesn't get auto-enabled when the handler option is used:

if (null === $handler['process_psr_3_messages']['enabled']) {
$handler['process_psr_3_messages']['enabled'] = !isset($handler['handler']) && !$handler['members'];
}

@HypeMC HypeMC force-pushed the add-sentry-breadcrumbhandler-support branch from 7d533ee to 10c35d5 Compare August 11, 2024 20:27
@HypeMC HypeMC force-pushed the add-sentry-breadcrumbhandler-support branch 5 times, most recently from ce023ed to 133cd1c Compare August 30, 2025 21:46
@HypeMC HypeMC force-pushed the add-sentry-breadcrumbhandler-support branch from 133cd1c to c469777 Compare September 5, 2025 22:37
@HypeMC HypeMC force-pushed the add-sentry-breadcrumbhandler-support branch from c469777 to 7572a98 Compare September 7, 2025 23:18
@GromNaN GromNaN changed the base branch from 4.x to 3.x September 26, 2025 18:34
@GromNaN
Copy link
Member

GromNaN commented Sep 26, 2025

Initially, I considered using the existing handler option instead of introducing the new sentry_handler option. However, I decided against it because the PSR logger doesn't get auto-enabled when the handler option is used

Should we make an exception for sentry_breadcrumb and always enable the the process_psr_3_messages handler?
Using both handler and sentry_handler with type: sentry_breadcrumb would not make sense, does it?

->booleanNode('persistent')->end() // socket_handler
->scalarNode('dsn')->end() // raven_handler, sentry_handler
->scalarNode('hub_id')->defaultNull()->end() // sentry_handler
->scalarNode('sentry_handler')->defaultNull()->end() // sentry_breadcrumb
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a handler setting used by all handlers that are wrapping another handler to configure which one it is. It should be reused inside of adding a new setting.

@stof
Copy link
Member

stof commented Sep 26, 2025

I don't think we should add support for third-party Monolog handlers in MonologBundle itself, as this will become unmaintainable:

  • how do we decide which one we implement ?
  • how do we deal with potential BC breaks in those packages for which we have no composer requirements ?

We already support type: service to allow defining a service yourselves and then using it as handler (potentially injected into other handlers), which is the way to support community handlers

@GromNaN
Copy link
Member

GromNaN commented Sep 26, 2025

The integration with the 3rd party library was added by #313. Either we decide to deprecate it in 3.x and remove it in 4.x, or we manage to integrate the features. I prefer the first solution for the rasons you exposed, but we need an upgrade path for users.

Edit: The documentation of the SentryBundle use the type: service configuration.
https://docs.sentry.io/platforms/php/guides/symfony/logs/

@HypeMC
Copy link
Member Author

HypeMC commented Sep 26, 2025

I agree that it makes sense not to support 3rd-party handlers. Sentry's is currently the only one, so it's an oddball.

However, I don't think 3rd-party handlers are the only issue. For example, this handler seems to be broken:

case 'rollbar':
    if (!empty($handler['id'])) {
        $rollbarId = $handler['id'];
    } else {
        $config = $handler['config'] ?: [];
        $config['access_token'] = $handler['token'];
        $rollbar = new Definition('RollbarNotifier', [
            $config,
        ]);
        $rollbarId = 'monolog.rollbar.notifier.'.sha1(json_encode($config));
        $rollbar->setPublic(false);
        $container->setDefinition($rollbarId, $rollbar);
    }

    $definition->setArguments([
        new Reference($rollbarId),
        $handler['level'],
        $handler['bubble'],
    ]);
    break;

The RollbarNotifier class doesn't exist in the current version of the rollbar/rollbar library, and Monolog v3's RollbarHandler doesn't support it either. I think referencing any 3rd-party classes in the code could lead to potential BC breaks, so maybe all of this should be removed?

@stof
Copy link
Member

stof commented Sep 26, 2025

@HypeMC this is because this code is related to support of the Monolog RollbarHandler of older versions of Monolog (which we'll be able to clean in 4.0).
Monolog itself has a policy to not accept new handlers in core that require third-party dependencies. But I think that we should keep support for those handlers in MonologBundle as long as they are part of Monolog itself.

@HypeMC
Copy link
Member Author

HypeMC commented Sep 26, 2025

@stof Yes, we should keep support for the handlers themselves. I was thinking we could maybe drop support for registering the 3rd-party classes and just keep the option of providing the service ID (in other words, drop the else part from my previous example).

@HypeMC
Copy link
Member Author

HypeMC commented Sep 29, 2025

Closing this one as we don't want to support third party handlers.

@HypeMC HypeMC closed this Sep 29, 2025
@HypeMC HypeMC deleted the add-sentry-breadcrumbhandler-support branch September 29, 2025 16:36
GromNaN added a commit that referenced this pull request Sep 30, 2025
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Deprecate `sentry` and `raven` handler types

| Q             | A
| ------------- | ---
| Branch?       | 3.x
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Issues        | Fix #489 (comment)
| License       | MIT

The upgrade path is to use a "service" handler with `sentry/sentry-symfony` as documented:
https://docs.sentry.io/platforms/php/guides/symfony/logs/

Commits
-------

1902f5e Deprecate `sentry` and `raven` handler types
GromNaN added a commit that referenced this pull request Sep 30, 2025
This PR was merged into the 4.x branch.

Discussion
----------

Remove `sentry` handler type

| Q             | A
| ------------- | ---
| Branch?       | 4.x
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #489 (comment)
| License       | MIT

Follow-up #537

Commits
-------

9dd3797 Remove "sentry" handler type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants