使用Slim时,所有错误都会正确记录,但会注意到
Notice: Undefined offset: 747 in /var/www/App.php on line 54
直到脚本结束才显示。当它们发生时,如何将它们记录到系统日志中?
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);
};
};