Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony命令的日志文件_Php_Shell_Symfony - Fatal编程技术网

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