Skip to content

Commit

Permalink
Add support for Drush errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Feb 17, 2021
1 parent 686506e commit c9e7abd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Add support for the `in_app_include` and `in_app_exclude` options
- Add all log messages as breadcrumbs
- Add support for Drush errors

### Fixed
- Replace recommended guzzlehttp/psr7 with php-http/guzzle6-adapter
Expand Down
15 changes: 14 additions & 1 deletion src/Logger/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Drupal\wmsentry\Event\SentryOptionsAlterEvent;
use Drupal\wmsentry\Event\SentryScopeAlterEvent;
use Drupal\wmsentry\WmsentryEvents;
use Drush\Drush;
use Psr\Log\LoggerInterface;
use Sentry\Breadcrumb;
use Sentry\ClientBuilder;
Expand All @@ -27,11 +28,13 @@
use Sentry\Serializer\Serializer;
use Sentry\Severity;
use Sentry\Stacktrace;
use Sentry\State\Hub;
use Sentry\State\Scope;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function Sentry\addBreadcrumb;
use function Sentry\captureEvent;
use function Sentry\captureException;
use function Sentry\withScope;

class Sentry implements LoggerInterface
Expand Down Expand Up @@ -74,6 +77,11 @@ public function __construct(
*/
$this->moduleHandler->loadInclude('wmsentry', 'module');
set_error_handler('_wmsentry_error_handler_real');

// Add Drush console error event listener.
if (class_exists(Drush::class) && method_exists(Drush::class, 'service')) {
Drush::service('eventDispatcher')->addListener(ConsoleEvents::ERROR, [$this, 'logDrush']);
}
}

public function onBeforeSend(Event $event): ?Event
Expand All @@ -98,6 +106,11 @@ public function log($level, $message, array $context = [])
$this->logBreadcrumb($level, $message, $context);
}

public function logDrush(ConsoleErrorEvent $event): void
{
captureException($event->getError());
}

protected function logBreadcrumb($level, $message, array $context): void
{
addBreadcrumb(Breadcrumb::fromArray([
Expand Down

0 comments on commit c9e7abd

Please sign in to comment.