对于 Symfony < 2.3
还有另一种可能性,覆盖应用程序类,下面的类可以帮助,这个应用程序类激活控制台的记录器并记录自动存在
//Acme/LoggingBundle/Console/Application.php
<?php
namespace Acme\LoggingBundle\Console;
use Symfony\Bundle\FrameworkBundle\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\DependencyInjection\IntrospectableContainerInterface;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\ArgvInput;
class Application extends BaseApplication
{
private $originalAutoExit;
public function __construct(KernelInterface $kernel)
{
parent::__construct($kernel);
$this->originalAutoExit = true;
}
/**
* Runs the current application.
*
* @param InputInterface $input An Input instance
* @param OutputInterface $output An Output instance
*
* @return integer 0 if everything went fine, or an error code
*
* @throws \Exception When doRun returns Exception
*
* @api
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
// make the parent method throw exceptions, so you can log it
$this->setCatchExceptions(false);
// store the autoExit value before resetting it - you'll need it later
$autoExit = $this->originalAutoExit;
$this->setAutoExit(false);
if (null === $input) {
$input = new ArgvInput();
}
if (null === $output) {
$output = new ConsoleOutput();
}
try {
$statusCode = parent::run($input, $output);
} catch (\Exception $e) {
/** @var $logger LoggerInterface */
$container = $this->getKernel()->getContainer();
$logger = null;
if($container instanceof IntrospectableContainerInterface){
$logger = $container->get('logger');
}
$message = sprintf(
'%s: %s (uncaught exception) at %s line %s while running console command `%s`',
get_class($e),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$this->getCommandName($input)
);
if($logger){
$logger->crit($message);
}
if ($output instanceof ConsoleOutputInterface) {
$this->renderException($e, $output->getErrorOutput());
} else {
$this->renderException($e, $output);
}
$statusCode = $e->getCode();
$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
}
if ($autoExit) {
if ($statusCode > 255) {
$statusCode = 255;
}
// log non-0 exit codes along with command name
if ($statusCode !== 0) {
/** @var $logger LoggerInterface */
$container = $this->getKernel()->getContainer();
$logger = null;
if($container instanceof IntrospectableContainerInterface){
$logger = $container->get('logger');
}
if($logger){
$logger->warn(sprintf('Command `%s` exited with status code %d', $this->getCommandName($input), $statusCode));
}
}
// @codeCoverageIgnoreStart
exit($statusCode);
// @codeCoverageIgnoreEnd
}
return $statusCode;
}
public function setAutoExit($bool)
{
// parent property is private, so we need to intercept it in a setter
$this->originalAutoExit = (Boolean) $bool;
parent::setAutoExit($bool);
}
}
然后在 app/console 中使用
//use Symfony\Bundle\FrameworkBundle\Console\Application;
use Acme\UltimateLoggingBundle\Console\Application;
至少删除 app/cache/* 目录 app/console cache:clear 还不够