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
Add a comment  | 

1 Answer 1

Reset to default 2

This 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