1

我有这个简单的控制台程序:

namespace MyApp\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;

class MaConsole extends Command {
 protected function configure()
 {  
    $this->setDescription('Console\'s not console');
 }

  protected function execute(
        \Symfony\Component\Console\Input\InputInterface $input,
        \Symfony\Component\Console\Output\OutputInterface $output
  ) {
    $output->writeln('Doing Stuff');
  }
}

我像这样加载它:

namespace MyApp;

use Symfony\Component\Console\Application as SymfonyApplication;
use MyApp\Console\MaConsole;

class Application extends SymfonyApplication
{
    public function __construct(
        string $name = 'staff',
        string $version = '0.0.1'
    ) {
        parent::__construct($name, $version);

        throw new \Exception('Test Sentry on Playground');
        $this->add(new MaConsole());
    }
}

我想在 Sentry 服务中记录上面抛出的异常。所以我的入口点是:

use MyApp\Application;

require __DIR__ . '/vendor/autoload.php';

Sentry\init([
    'dsn' => getenv('SENTRY_DSN'),
    'environment' => getenv('ENVIRONMENT')
]);

$application = (new Application())->run();

但是即使我设置了正确的环境变量,我也无法将错误记录到哨兵中。

该应用程序不加载完整的 Symfony 框架,而是只使用控制台组件,所以我不知道是否应该使用 Sentry Symfony 集成:https ://docs.sentry.io/platforms/php/symfony/

原因是因为我不知道如何在我的情况下加载包,因此我使用 SDK。

编辑1:

我还尝试捕获异常并手动记录它,但由于某些原因也没有记录:

use MyApp\Application;

require __DIR__ . '/vendor/autoload.php';

try {
  Sentry\init([
    'dsn' => getenv('SENTRY_DSN'),
    'environment' => getenv('ENVIRONMENT')
  ]);
  throw new \Exception('Test Sentry on Playground');

  $application = (new Application())->run();
} catch(Exception $e) {
    Sentry\captureException($e);
}
4

1 回答 1

1

您可以使用调度程序:

use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) use ($env) {
    Sentry\init([
        'dsn' => getenv('SENTRY_DSN'),
        'environment' => $env
    ]);
    Sentry\captureException($event->getError());
});

$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->setDispatcher($dispatcher);
$application->run($input);
于 2020-02-26T10:08:24.057 回答