15

How to disable profiler in Symfony2 in production?

I do not mean the toolbar - I mean the profiler.

I want to disable it in production, I use it extensively for development so the solution with removing its bundle is a no-go.

I have tried setting framework.profiler.only_exceptions to true. I have tried removing the framework.profiler section altogether. No matter what the profiler.db is growing after every request and every response contains x-debug-token header.

I have double-checked the config files (config.yml and config_prod.yml) and everything seems to be fined.

What's more the command app/console router:dump-apache --no-debug always dumps the _wdt and _profiler routes, but I don't have them in my routing_prod.yml and they don't seem to be present when trying to access them from the browser (404).

I'm running symfony 2.0 and I won't upgrade right now because of some major changes in 2.1 which would require a rewrite of many elements. It wouldn't be wise to start it just before initial deployment.

3
  • My config files (some things [REMOVED] for privacy/security reasons): config_prod.yml, config.yml
    – pinkeen
    Commented Dec 16, 2012 at 14:51
  • Check your appKernel.php file: The WebProfilerBundle isn't loaded in production mode unless you changed it previously. To activate prod-mode use app.php and clear everything using: console cache:clear --env=prod --no-debug
    – Lin-Art
    Commented Dec 18, 2012 at 10:22
  • I checked and you're right that there is a similar condition in AppKernel.php. This is how it was from the start if(in_array($this->getEnvironment(), array('dev', 'test'))). The strange thing is that the profiler is loaded nevertheless. I shall investigate when have the time.
    – pinkeen
    Commented Dec 21, 2012 at 10:50

3 Answers 3

15

Symfony >= 2.2

As of Symfony 2.2 the profiler supports an enabled flag in the framework's configuration and is disabled by default in the test environment.

# app/config/config_test.yml
framework:
    profiler:
        enabled: false

See this Blog entry about Profiling by Fabien Potencier and the FrameworkBundle configuration reference for more details.

Update: This flag is still valid in Symfony 4.0.


Symfony <= 2.1

In Symfony <= 2.1 The profiler is disabled entirely if there's no framework.profilerkey in the configuration.

You can see this in the ProfilerPass of the Symfony2 FrameworkBundle configuration.

This is the case for the default config.yml and config_prod.yml (which includes the former). So if you didn't tinker with the default configurations you're fine.

In config_dev.yml however the default setting is:

framework:
    profiler: { only_exceptions: false }

Which enables profiling for the dev environment and all enviroments that import config_dev.yml like config_test.yml.

If you want to unset the profiler value in a subsequent configuration use:

framework:
    profiler: false

Values like {} or ~ won't unset the value. You have to use false.

2

Did you try this (enable only for development)

As the profiler adds some overhead, you might want to enable it only under certain circumstances in the production environment. The only-exceptions settings limits profiling to 500 pages, but what if you want to get information when the client IP comes from a specific address, or for a limited portion of the website? You can use a request matcher:

framework:
    profiler:
        matcher: { ip: 192.168.0.0/24 }

http://symfony.com/doc/current/book/internals.html#profiler

or

the profiler can be disabled on a per-action basis by doing something like:

if(in_array($this->container->get('kernel')->getEnvironment(), array('prod'))) {
    $this->container->get('profiler')->disable();
}
2
  • The only_exceptions settings doesn't work at all. Profiler is being run also for 200 pages. I'll try your solution, but even if it works it's not perfect - there will still be some overhead from setting up the profiler and ip matching. The second solution seems a bit clunky and I'm not sure it will get rid of the unnecessary overhead completely. Where did you find it? I don't see it in the link provided. Thanks, I will get back to you.
    – pinkeen
    Commented Dec 16, 2012 at 14:28
  • It's really strange. Enabling the IP matching didn't help at all. The profiler is still there. I will examine the conf. once again and maybe post it here.
    – pinkeen
    Commented Dec 16, 2012 at 14:37
1

I figured it out, but still I'm not sure why the profiler settings didn't work. I did clear the cache with --no-debug after each change of the configuration.

Firstly I examined the Configuration of FrameworkBundle and found out that profiler conf node has canBeDisabled(). Then I checked what does it mean exactly.

It turns out that each canBeDisabled node has an implied child node enabled with default value set to true. You can either override it or set the parent node directly to false or null to disable the section. If you simply omit the profiler section then it is enabled by default.

Maybe I missed it in the docs, but I'm pretty sure it should be mentioned here. Also, in my opinion profiler should be disabled by default in production. I can't imagine a scenario when it would be beneficial to run profiler in production in the long run. I'll be happy if anybody proves me wrong.

BTW I noticed then as the profiler.db grows then each request becomes slower, but that may not be the case in prod.

6
  • You usually need two "kinds" of production. One being the real thing, and the other one for testing it in the same conditions (in the same server or a replica), usually called 'staging'. In 'staging', you want it to use the 'prod' environment, but with debugging enabled. If an error occurs, you want to have as much data as possible. Debug should also be true in the app.php file, and in the monolog configuration. Commented Dec 17, 2012 at 11:01
  • 1
    Also, all commands should be run with --env=prod in production (which I think also enables --no-debug automatically) because it's faster. Commented Dec 17, 2012 at 11:04
  • I agree, you have a good point. But if you use staging then you should use a third environment that possibly inherits most of the config from production. There is only one 'production' and in most cases the environment should be fully optimized. I somehow missed the --env=prod part (maybe this was causing some problems?). I thought for some reason that --no-debug assumes prod environment.
    – pinkeen
    Commented Dec 17, 2012 at 11:55
  • This is only true for Symfony 2.2. Your link leds to the Master-Branch, which is 2.2 right now. In 2.0 (with which your question is tagged with) there's no such option (see: github.com/symfony/symfony/blob/2.0/src/Symfony/Bundle/…). So I still don't know how to disable the profiler.
    – flu
    Commented Jan 30, 2013 at 11:04
  • 1
    @ChocoDeveloper: Your comment stood the test of time, and solved an issue in year 2021 that all other solutions could not. THANKS! Commented Mar 1, 2021 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.