Symfony 命令的日志文件

Gay*_*d.P 0 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;

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);

    }
}
Run Code Online (Sandbox Code Playgroud)

如果可能的话,如果有错误,请给我发送电子邮件:) 谢谢:)

Ami*_*UIS 5

我遇到了同样的问题,所以我这样解决了:

尝试为特定日志记录通道创建处理程序以将日志存储在不同的文件中。在这里我定义了security频道作为示例。

/!\ 您的 Command 类必须扩展ContainerAwareCommand才能用于$this->getContainer() 获取此参数kernel.logs_dir。因此,这一行将$this->getContainer()->getParameter('kernel.logs_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');
}
Run Code Online (Sandbox Code Playgroud)

您的日志文件将存储在/var/www/html/project-name/var/logs