Andrei Herford Andrei Herford - 1 year ago 104
PHP Question

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

I am working on my first Symfony based WebApp project. I have configured Symfony to not only write log messages to the different log files but also to send critical error messages immediately as e-mail. This works fine. However I would like to add some additional information to the default log messages to make it easier to find the actual error source.

The Twig file of one pages loads the localized text from a .yml file. The texts contains an

placeholder that should be replaced by the route/URL to the About page. I forgot to this replacement, so the link did not point to an URL but to
instead. This leads to an
since no rout to
can be found...

No big deal. But to find the actual page/controller that contains this error was a bit tricky. 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 []

So the exception was thrown in
when trying to find a route to
. Fine, this does not give me any hint on which page this bad link is located.

Of course the call to the bad route does not have to be located on any page at all. The user could have entered the bad link directly. Symfony would have to store/remember the last page to give any hint about the possible source. So, is it possible to include this information at all?

Additionally I would like to add information about the Host the problem was reported on. I am running two instances of the WebApp:
and it would be a great help, if the log message would show if it comes from
or from

Adding this information to log messages I create on myself is no problem, but how can I add this information to messages generated by Symfony or third party code? I would have to intercept the log message somehow before it reaches the log handler. Is this possible?

Answer Source

If you want to add extra information to log entries, you can do this with a processor. With a processor you can modify the record array, before it's parsed by a formatter. The extra part is shown at the end of the log entry.


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();

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

        return $record;

Now add it to your services.yml to register it for all log entries:

    class: AppBundle\Monolog\WebProcessor
    arguments: ["@request_stack"]
        - { name: monolog.processor, method: processRecord }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download