简体   繁体   中英

Logger does not log Exceptions thrown in application

I use the configuration below to create a logger:

'log' => [
    'logger' => [
        'writers' => [
            'stream' => [
                'name' => 'stream',
                'priority' => \Zend\Log\Logger::INFO,
                'options' => [
                    'stream' => __DIR__ . '/../../data/log/name_' . date("Ym") . '.log',
                    'formatter' => [
                        'name' => \Zend\Log\Formatter\Simple::class,
                        'options' => [
                            'format' => '%timestamp% %priorityName% : %message% %extra%',
                            'dateTimeFormat' => 'c',
                        ],
                    ],

                ],
            ],
        ],
        'processors' => [
            'requestid' => [
                'name' => \Zend\Log\Processor\RequestId::class,
            ],
            'backtrace' => [
                'name' => \Zend\Log\Processor\Backtrace::class,
            ],
        ],
    ],
],

Log works fine but php error not logged nowhere.

Why?

I use this code to generate a simple error

$d1 = new DateTime();
$d2 = new DateTime();
$a = array($d1,$d2);
$a->format("Y-m-d");

in my controller class.

This error is not logged

In PHP 7 ther's Error class to catch Fatal error

}catch(\Error $err){
    $this->log->err($err->getMessage());

Error class

zend-log is not exception or error handler by default. You have to set it to do that. You can use exceptionhandler , errorhandler and fatal_error_shutdownfunction options to set it. All config keys accepts boolean value. Here's an example. I didn't test it but should be like that.

'log' => [
    'logger' => [
        'writers' => [
            'stream' => [
                'name' => 'stream',
                'priority' => \Zend\Log\Logger::INFO,
                'options' => [
                    'exceptionhandler' => true,
                    'errorhandler' => true,
                    'stream' => __DIR__ . '/../../data/log/name_' . date("Ym") . '.log',
                    'formatter' => [
                        'name' => \Zend\Log\Formatter\Simple::class,
                        'options' => [
                            'format' => '%timestamp% %priorityName% : %message% %extra%',
                            'dateTimeFormat' => 'c',
                        ],
                    ],
                ],
            ],
        ],
        'processors' => [
            'requestid' => [
                'name' => \Zend\Log\Processor\RequestId::class,
            ],
            'backtrace' => [
                'name' => \Zend\Log\Processor\Backtrace::class,
            ],
        ],
    ],
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM