Php Symfony命令的日志文件
我想将所有返回的“命令”文件保存在日志文件中Php Symfony命令的日志文件,php,shell,symfony,Php,Shell,Symfony,我想将所有返回的“命令”文件保存在日志文件中 <?php namespace LogicielBundle\Command\Cron; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; clas
<?php
namespace LogicielBundle\Command\Cron;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MailRapportDometechCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('logiciel:mail_rapport_preparation_dometech')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('. <bg=black;fg=white>Log color ...</>');
$logFile = fopen('app/CommandLogs/MailRapportDometech/compteur.txt', 'w+');
fputs($logFile, 'Test ... how to have $output data ?');
fclose($logFile);
}
}
您必须使用记录器
服务,而不是直接写入文件。就为了这些动作,Monolog捆绑包被集成到Symfony的核心中
例如:
$this->getContainer()->get('logger')->error('your message');
在您的app/config/config.yml
中,您可以为每个级别自定义处理程序
monolog:
handlers:
applog:
type: stream
path: /path/to/your/file.log
level: error
在文档中,您可以找到如何创建电子邮件处理程序。您必须使用logger
服务,而不是直接写入文件。就为了这些动作,Monolog捆绑包被集成到Symfony的核心中
例如:
$this->getContainer()->get('logger')->error('your message');
在您的app/config/config.yml
中,您可以为每个级别自定义处理程序
monolog:
handlers:
applog:
type: stream
path: /path/to/your/file.log
level: error
在文档中,您可以找到如何创建电子邮件处理程序。我也遇到过同样的问题,所以我这样解决了它:
尝试为特定日志通道创建处理程序,以将日志存储在不同的文件中。这里我定义了security
channel作为一个例子
/!\为了使用$this->getContainer()
获取此参数kernel.logs\u dir
,您的命令类必须扩展containerWareCommand。因此
这一行$this->getContainer()->getParameter('kernel.logs\u dir')
将为您提供日志目录的路径
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class CheckCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
// create a log channel
$log = new Logger('security');
$log->pushHandler(new StreamHandler($this->getContainer()->getParameter('kernel.logs_dir').'/file.log', Logger::DEBUG));
$log->addWarning('Foo');
$log->addError('Bar');
}
您的日志文件将存储在/var/www/html/project name/var/logs我遇到了同样的问题,所以我这样解决了它:
尝试为特定日志通道创建处理程序,以将日志存储在不同的文件中。这里我定义了security
channel作为一个例子
/!\为了使用$this->getContainer()
获取此参数kernel.logs\u dir
,您的命令类必须扩展containerWareCommand。因此
这一行$this->getContainer()->getParameter('kernel.logs\u dir')
将为您提供日志目录的路径
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class CheckCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
// create a log channel
$log = new Logger('security');
$log->pushHandler(new StreamHandler($this->getContainer()->getParameter('kernel.logs_dir').'/file.log', Logger::DEBUG));
$log->addWarning('Foo');
$log->addError('Bar');
}
您的日志文件将存储在/var/www/html/project name/var/logs非常感谢,但此方法无法保存PHP错误:-/您不正确。Symfony调试组件处理所有错误并将其写入多个通道。您可以在生产环境中启用它,而无需向最终用户Debug::enable(null,false)报告;非常感谢,但此方法无法保存PHP错误:-/您不正确。Symfony调试组件处理所有错误并将其写入多个通道。您可以在生产环境中启用它,而无需向最终用户Debug::enable(null,false)报告;