Reputation: 11
I have URL /init-stream?id=... in my app, it is used by some external application(s). It should answer OK or some error code. If id parameter is absent, the response should be ERROR_NO_ID. So, my code is
$app->get('/init-stream', function(Request $request) use ($app) {
$idParam = $request->get('id');
if ($idParam === NULL) {
return new Response('ERROR_NO_ID', 400);
}
...
});
Everything is ok, but I want to log this situation as WARNING, because it can help me to debug the application that invokes this request. So, I change the code to:
$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);
});
Everything works, but there is one thing I do not like. My log looks like:
[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) [] []
There is nothing critical in my app. How can I get rid of this CRITICAL message?
Upvotes: 1
Views: 470
Reputation: 93
Short answer: the priority of the error-callback is to low. You have to use a priority >= -4. For example:
$app->error(function(App\Exception\BadRequestException $e) use ($app) {
$app['logger']->addWarning(...);
return new Response($e->getMessage(), $e->getCode);}, -4); // <= !!!this
Info: The KernelEvents::EXCEPTION is earlier then your error callback. This will trigger the class Silex\EventListener\LogListener::onKernelException, and so on
Upvotes: 1
Reputation: 3760
This is due to the monolog log level. The current log levels supported are:
DEBUG will log everything, INFO will log everything except DEBUG, NOTICE will log everthing except DEBUG and INFO and so on with the chain.
So to avoid CRITICAL log messages, log level should be set to ALERT or EMERGENCY.
Monolog classifies exception under CRITICAL log level and adds it to your log file whenever exception occurs and you have log level set to include CRITICAL log messages. I recommend to use DEBUG level for development and from ERROR level downwards for production. Also set $app['debug']=false on production.
Upvotes: 0