简体   繁体   English

Symfony / Monolog:如何自定义关于404找不到异常的日志的格式

[英]Symfony/Monolog: how to customize the format of logs about 404 not found exceptions

Monolog offers a way to exclude 404 errors from the log , but what if I want to customize them instead of totally suppressing them? Monolog提供了一种从日志中排除404错误的方法,但是如果我想自定义它们而不是完全消除它们怎么办?

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)"} [] [2016-12-12 10:41:01] request.ERROR:未捕获的PHP异常Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException:“在/ myproject / vendor /中找不到“ GET / non-existing-route”的路由” symfony / symfony / src / Symfony / Component / HttpKernel / EventListener / RouterListener.php第125行{“ exception”:“ [对象](Symfony \\ Component \\ HttpKernel \\ Exception \\ NotFoundHttpException(code:0):找不到\\的路由”在/myproject/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php:125、Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(代码:0)处获取GET / non-existing-route \\“:在/myproject/var/cache/dev/appDevDebugProjectContainerUrlMatcher.php:1181)“} []

Assuming that you're talking about the log life in app/logs directory. 假设您正在谈论app / logs目录中的日志寿命。 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 示例: 在symfony中创建自定义的monolog记录器和格式化程序

  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. 然后,我将在自定义事件侦听器中捕获NotFoundHttpException异常(例如: 404 NotFoundHttpException发生时,使用onKernelException触发事件侦听器中的重定向 ),并使用在步骤1中创建的自定义记录器将其记录下来。

Note: Your custom logger is injected in controller in step 1 above. 注意:您的自定义记录器已在上面的步骤1中注入控制器中。 In your case, inject it to your event listener. 在您的情况下,将其注入事件监听器。

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. 然后在进程内部或检测到NotFoundHttpException并将其替换为人类可读的消息。

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

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