How to add additional information (host, URL, etc.) to the output of the Symfony / Monolog log?

I am working on my first Symfony-based WebApp project. I configured Symfony not only to write log messages to different log files, but also to send critical error messages right away via email. It works great. However, I would like to add additional information to the default log messages to make it easier to find the actual source of the errors.

Example: A Twig file on one page loads localized text from a .yml file. The texts contain a placeholder %about_link%that must be replaced by the route / URL on the About page. I forgot this replacement, so the link did not point to the URL, but instead %about_link%. This results in NotFoundHttpExceptiona route cannot be found %about_link%...

No big deal. But finding the actual page / controller containing this error was a bit difficult. The default log message shows the following:

[2015-12-14 17:19:36] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /%25about_link%25"" at /long/path/to/symfony/.../RouterListener.php line 176 []

Thus, when trying to find a route to the %about_link%exception was selected at RouterListener.php. Well, that doesn't give me a hint on which page this bad link is located.

Of course, calling a bad route does not have to be placed on any page. The user could enter a bad link directly. Symfony will store / remember the last page to give any hint of a possible source. So, is it possible to include this information at all?

In addition, I would like to add information about the host that was reported. I am launching two instances of WebApp: www.my_web_app.xyand betatest.my_web_app.xy, and that would be a big help if the log message was displayed if it comes from wwwor from betatest.

, , , , Symfony ? - , . ?

+4
3

, . , . .

<?php

namespace AppBundle\Monolog;

use Symfony\Component\HttpFoundation\RequestStack;

class WebProcessor
{
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function processRecord(array $record)
    {
        $request = $this->requestStack->getCurrentRequest();

        if ($request) {
            $record['extra']['host'] = $request->getHost();
            $record['extra']['url'] = $request->getRequestUri();
            // ...
        }

        return $record;
    }
}

services.yml, :

app.monolog.processor.web:
    class: AppBundle\Monolog\WebProcessor
    arguments: ["@request_stack"]
    tags:
        - { name: monolog.processor, method: processRecord }
+8

! WebProcessor, .

, , monolog.processor:

# app/config/services.yml
services:
    Monolog\Processor\WebProcessor:
        tags: ['monolog.processor']

, . :

# app/config/services/monolog.yml (I included services/*.yml in config.yml)
services:
    _defaults:
        tags: ['monolog.processor']
    Monolog\Processor\WebProcessor: ~
    Monolog\Processor\GitProcessor: ~
    Monolog\Processor\MemoryUsageProcessor: ~
    Monolog\Processor\MemoryPeakUsageProcessor: ~
    Monolog\Processor\IntrospectionProcessor: ~
+1

, , monolog. : http://symfony.com/doc/current/cookbook/logging/monolog.html#changing-the-formatter

Short version: you can create your own formatting class that implements Monolog \ Formatter \ FormatterInterface, and you can include it in the config.yml file as follows:

# app/config/config.yml
services:
    my_formatter:
        class: Monolog\Formatter\JsonFormatter
monolog:
    handlers:
        file:
            type: stream
            level: debug
            formatter: my_formatter
0
source

Source: https://habr.com/ru/post/1620064/


All Articles