admin管理员组文章数量:1292629
I'm working on a Symfony 4.4 console command and I want to prevent exceptions from being displayed on the terminal. I've already set up a logger to log the exceptions, but they still appear on the terminal. I've also enabled production mode, but the issue persists.
Here is the code
<?php
namespace App\Command;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SampleCommand extends Command
{
protected static $defaultName = 'SampleCommand';
protected static $defaultDescription = 'Add a short description for your command';
private $logger;
public function __construct(LoggerInterface $logger)
{
parent::__construct();
$this->logger = $logger;
}
protected function configure(): void
{
$this
->setDescription(self::$defaultDescription)
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
throw new \Exception('This is a built-in exception message.');
} catch (\Exception $exception) {
$this->logger->critical('An error occurred', [
'exception' => $exception,
]);
}
}
}
I tried logging it but error still getting generated on terminal.
I'm working on a Symfony 4.4 console command and I want to prevent exceptions from being displayed on the terminal. I've already set up a logger to log the exceptions, but they still appear on the terminal. I've also enabled production mode, but the issue persists.
Here is the code
<?php
namespace App\Command;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SampleCommand extends Command
{
protected static $defaultName = 'SampleCommand';
protected static $defaultDescription = 'Add a short description for your command';
private $logger;
public function __construct(LoggerInterface $logger)
{
parent::__construct();
$this->logger = $logger;
}
protected function configure(): void
{
$this
->setDescription(self::$defaultDescription)
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
throw new \Exception('This is a built-in exception message.');
} catch (\Exception $exception) {
$this->logger->critical('An error occurred', [
'exception' => $exception,
]);
}
}
}
I tried logging it but error still getting generated on terminal.
Share Improve this question asked Feb 13 at 7:51 DL coderDL coder 11 bronze badge 3- Which exception do you see? – Olivier Commented Feb 13 at 8:43
- this one still getting displayed on terminal $this->logger->critical('An error occurred', [ 'exception' => $exception, ]);. but i want to prevent it for security purpose. i just want to be logged not display on the terminal – DL coder Commented Feb 13 at 9:14
- 1 Then it means your logger is configured to log to the standard output and not to a file. – Olivier Commented Feb 13 at 9:19
1 Answer
Reset to default 2This is completely normal behavior: logging to the console for console commands.
The standard console logger is very minimalistic, making it difficult to achieve what you need.
However, if you install Monolog, you can suppress log output in the prod environment by simply adding the line
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
in the execute
method before the first console output method is called (before the first log entry). It’s best to place it at the very beginning to avoid confusion. However, keep in mind that -v
, -vv
, and -vvv
options will have no effect on the command, and you’ll need to handle these arguments separately.
本文标签: phpPrevent exceptions from being displayed on the terminal in Symfony 44Stack Overflow
版权声明:本文标题:php - Prevent exceptions from being displayed on the terminal in Symfony 4.4 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741557701a2385257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论