Symfony News

New in Symfony 4.3: Improved HttpCache logging

Matthias Pigulla

Contributed by
Matthias Pigulla
in #30964.

The Symfony Reverse Proxy provided by the HttpCache feature is a gateway cache written in PHP. It's not a fully-featured reverse proxy like Varnish, but it can provide a big performance boost without having to install, configure and maintain additional applications or services. That's why some projects use it even in production.

In Symfony 4.3 we've added new configuration options to improve the logging of HttpCache responses. First, the trace_level option tells Symfony which logging information must be included in the response via the X-Symfony-Cache HTTP header:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// src/CacheKernel.php
namespace App;

use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;

class CacheKernel extends HttpCache
{
    protected function getOptions()
    {
        return [
            // ...

            // 'none'  = log nothing
            // 'short' = log only the main request
            // 'full'  = log the main request and all the sub-requests
            // default value = 'full' if debug = true; 'none' otherwise
            'trace_level' => 'short',
        ];
    }
}

The logged value is concise and ready for machine processing (e.g. stale/valid/store). If you use Apache, add %{X-Symfony-Cache}o to the LogFormat directive to include this value in the generated logs.

A use case for this feature is to enable it in production and use a tool to process the generated log info to find the slowest URLs, the ones that need extra cache tweaking, etc.

If you prefer to rename the X-Symfony-Cache HTTP header, use the trace_header option:

1
2
3
4
5
6
7
8
protected function getOptions()
{
    return [
        // ...
        'trace_level' => 'short',
        'trace_header' => 'X-Cache-Info-Log',
    ];
}

Fun fact: this pull request adds the first usage of the array_key_first() PHP function in Symfony codebase. This function was introduced in PHP 7.3 but if you use a lower PHP version, the Polyfill PHP 7.3 component will provide that function for you.


Be trained by Symfony experts - 2019-04-25 Clichy - 2019-04-25 Lille - 2019-04-29 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook