为什么symfony 2.7没有记录我的控制器的通知

时间:2016-01-21 11:58:21

标签: php symfony

我有行动:

public function getQuestions2Action()
{
    MY_CONSTANT;
}

应用程序/配置/ config_prod.yml

imports:
    - { resource: config.yml }

monolog:
    handlers:
        main:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            channels: "!event"

在/var/log/nginx/error.log中,我看到了:

2016/01/21 14:39:48 [error] 31596#0: *313 FastCGI sent in stderr: "PHP message: PHP Notice:  Use of undefined constant MY_CONSTANT - assumed 'MY_CONSTANT' in /var/www/datravel-client-api/src/Datravel/ClientApiBundle/Controller/RestController.php on line 289
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /var/www/datravel-client-api/web/app.php:0
PHP message: PHP   2. Symfony\Component\HttpKernel\Kernel->handle($request = *uninitialized*, $type = *uninitialized*, $catch = *uninitialized*) /var/www/datravel-client-api/web/app.php:27
PHP message: PHP   3. Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle($request = *uninitialized*, $type = *uninitialized*, $catch = *uninitialized*) /var/www/datravel-client-api/app/bootstrap.php.cache:2442
PHP message: PHP   4. Symfony\Component\HttpKernel\HttpKernel->handle($request = *uninitialized*, $type = *uninitialized*, $catch = *uninitialized*) /var/www/datravel-client-api/app/bootstrap.php.cache:3223
PHP message: PHP   5. Symfony\Component\HttpKernel\HttpKernel->handleRaw($request = *uninitialized*, $type = *uninitialized*) /var/www/datravel-client-api/app/bootstrap.php.cache:3072
PHP message: PHP   6. call_user_func_array:{/var/www/datravel-client-api/app/bootstrap.php.cache:3110}(*uninitialized*, *uninitialized*) /var/www/datravel-client-api/app/bootstrap.php.cache:3110
PHP message: PHP   7. Datravel\ClientApiBundle\Controller\RestController->getQuestions2Action() /var/www/datravel-client-api/app/bootstrap.php.cache:3110" while reading response header from upstream, client: 127.0.0.1, server: datravel-client-api.local, request: "GET /api/v1/client/questions2.json HTTP/1.1", upstream: "fastcgi://127.0.0.1:9400", host: "datravel-client-api.local"

但是app / logs / prod.log是空的

$ ls -la app/logs/
total 8
drwxrwxrwx 2 lebnik lebnik 4096 дек.  18 14:02 .
drwxrwxr-x 6 lebnik lebnik 4096 янв.  21 12:59 ..
-rw-rw-r-- 1 lebnik lebnik    0 июля  31 17:20 .gitkeep
-rwxrwxrwx 1 lebnik lebnik    0 дек.  18 14:02 prod.log

Prod缓存被清除。我期待关于未定义常量的记录通知。

$ php5-fpm -v

PHP 5.6.11-1ubuntu3.1 (fpm-fcgi)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans

2 个答案:

答案 0 :(得分:3)

与应用程序在"调试模式"中运行的dev环境不同。 (这意味着symfony本身将error_reporting设置为E_ALL并且所有错误都转换为异常并记录),在prod环境error_reporting中是您已在php.ini中设置了php错误,symfony 不会处理。然而,PHP会将它们记录在error_log中指定的文件中,通常是/var/log/httpd/error_log/var/log/apache2/error.log

如果你想在生产中处理php错误以通过monolog记录它们,你可以注册Symfony\Component\HttpKernel\EventListener\DebugHandlersListener监听器。在您的服务中:

 debug.debug_handlers_listener:
     class: Symfony\Component\HttpKernel\EventListener\DebugHandlersListener
     arguments: [~, "@logger", 24575, ~, false]
     tags:
         -  { name: kernel.event_subscriber}

24575E_ALL ^ E_DEPRECATED的输出。

更新

我认为,既然两个环境中都有debug.debug_handlers_listener already exists,你应该像这样创建一个编译器传递:

namespace TestBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DebugHandlersCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        if (!$container->getParameter('kernel.debug')) {
            $definition = $container->findDefinition('debug.debug_handlers_listener');

            // by default is E_COMPILE_ERROR | E_PARSE | E_ERROR | E_CORE_ERROR
            $definition->replaceArgument(2, E_ALL ^ E_DEPRECATED);
            $definition->replaceArgument(4, false);
        }
    }
}

然后像这样注册:

namespace TestBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use TestBundle\DependencyInjection\Compiler\DebugHandlersCompilerPass;

class TestBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // add this
        $container->addCompilerPass(new DebugHandlersCompilerPass());
    }
}

您现在需要做的就是将action_level: error文件中monolog的{​​{1}}更改为config_prod.yml

答案 1 :(得分:0)

您在/var/log/nginx/error.log中收到错误,说明未定义MY_CONSTANT。 我不知道你的代码在哪里定义它(可能在超类或你包含的其他类中?),但我认为除了“你没有定义它”之外你不能获得任何信息,除非PHP解析器找不到它一个合适的地方。 也许你可以搜索你的项目并找到你在哪里定义这个'MY_CONSTANT'的形式   const MY_CONSTANT = xxx