2
votes

Monolog offers a way to exclude 404 errors from the log, but what if I want to customize them instead of totally suppressing them?

So showing something different than:

[2016-12-12 10:41:01] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /non-existing-route"" at /myproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 125 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): No route found for \"GET /non-existing-route\" at /myproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:125, Symfony\Component\Routing\Exception\ResourceNotFoundException(code: 0): at /myproject/var/cache/dev/appDevDebugProjectContainerUrlMatcher.php:1181)"} []

2
Do you mean the way it is logged in log file?BentCoder

2 Answers

2
votes

The possible solution is to use log processor.

The detailed documentation is at How to Add extra Data to Log Messages via a Processor

Then inside the process or detect the NotFoundHttpException and replace it with human-readable message.

2
votes

Assuming that you're talking about the log life in app/logs directory. If so, I would do it this way which is pretty straight forward.

  1. Create a custom logger and associated log formatter. Example: Creating a custom monolog logger and formatter in symfony

  2. I would then catch NotFoundHttpException exception in a custom event listener (Example: Triggering redirect in event listener with onKernelException when 404 NotFoundHttpException occurs) and log it with my custom logger that I created in step 1.

Note: Your custom logger is injected in controller in step 1 above. In your case, inject it to your event listener.