【发布时间】:2015-12-24 08:49:55
【问题描述】:
我的应用中有 URL /init-stream?id=...,它被一些外部应用程序使用。它应该回答 OK 或一些错误代码。如果没有 id 参数,则响应应为 ERROR_NO_ID。所以,我的代码是
$app->get('/init-stream', function(Request $request) use ($app) {
$idParam = $request->get('id');
if ($idParam === NULL) {
return new Response('ERROR_NO_ID', 400);
}
...
});
一切正常,但我想将此情况记录为警告,因为它可以帮助我调试调用此请求的应用程序。所以,我把代码改成:
$app->get('/init-stream', function(Request $request) use ($app) {
$idParam = $request->get('id');
if ($idParam === NULL) {
throw new App\Exception\BadRequestException('ERROR_NO_ID', 400);
}
...
});
$app->error(function(App\Exception\BadRequestException $e) use ($app) {
$app['logger']->addWarning(...);
return new Response($e->getMessage(), $e->getCode);
});
一切正常,但有一件事我不喜欢。我的日志看起来像:
[2015-12-22 16:20:00] myapp.CRITICAL: App\Exception\BadRequestException: ERROR_NO_ID (uncaught exception) at C:\Users\yy\onetimelink\public\index.php line 166 {"exception":"[object] (App\\Exception\\BadRequestException(code: 400): ERROR_NO_ID at C:\\Users\\yy\\onetimelink\\public\\index.php:166)"} []
[2015-12-22 16:20:00] myapp.WARNING: GET /init-stream?sex=male&age=33 : ERROR_NO_ID (C:\Users\yy\onetimelink\public\index.php:166) [] []
我的应用程序中没有任何重要内容。我怎样才能摆脱这个 CRITICAL 消息?
【问题讨论】:
标签: php exception silex error-logging