繁体   English   中英

记录PHP通知

[英]Logging PHP notices

使用Slim,我的所有错误均被正确记录,但出现诸如<b>Notice</b>: Undefined offset: 747 in <b>/var/www/App.php</b> on line <b>54</b><br />直到脚本结束才显示。 发生时如何将它们记录到syslog?

<?php
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set("log_errors", 1);
ini_set('display_errors', 1);
openlog('PublicAPI', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER | LOG_PERROR);

require '../vendor/autoload.php';

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App([
    'displayErrorDetails'=>true,
    'settings' => parse_ini_file(__DIR__.'/../config.ini', true, INI_SCANNER_TYPED)
]);

$c = $app->getContainer();

$c['errorHandler'] = function ($c) {
    return function ($request, $response, $e) use ($c) {
        syslog(LOG_ERR, "Slim errorHandler: {$e->getFile()} ({$e->getline()}): {$e->getMessage()} ({$e->getCode()})");
        return $c['response']->withJson(['message'=>'Server Error'],500);
    };
};
$c['notFoundHandler'] = function ($c) {
    return function ($request, $response) use ($c) {
        syslog(LOG_ERR, 'Slim notFoundHandler error handler');
        return $c['response']->withJson(['message'=>'Page not found'],404);
    };
};
$c['notAllowedHandler'] = function ($c) {
    return function ($request, $response, $methods) use ($c) {
        $uri=$request->getUri();
        $path=$uri->getPath();
        syslog(LOG_ERR, "Slim notAllowedHandler error handler.  $uri $path Method must be one of: " . implode(', ', $methods));
        return $c['response']
        ->withHeader('Allow', implode(', ', $methods))
        ->withJson(['message'=>'Method must be one of: ' . implode(', ', $methods)],405);
    };
};
$c['phpErrorHandler'] = function ($c) {
    return function ($request, $response, $e) use ($c) {
        $msg="Slim phpErrorHandler: {$e->getFile()} ({$e->getline()}): {$e->getMessage()} ({$e->getCode()})";
        syslog(LOG_ERR, $msg);
        return $c['response']->withJson(['message'=>$msg],500);
    };
};

这样的事情会起作用:

set_error_handler(function ($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting, so ignore it
        return false;
    }
    syslog(LOG_ERR, "PHP error: $file ($line}): {$message} ({$severity})");
    return true;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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