craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[4.x]: Debug toolbar database/db item missing outside of dev mode #11820

Closed turnstylerj closed 2 years ago

turnstylerj commented 2 years ago

What happened?

Description

When devMode is true, and the debug toolbar is enabled on the front end for the current user, there is a "db" item showing the amount of database queries run: Screen Shot 2022-08-23 at 10 32 48 AM

When 'devMode' is set to false (via an environment variable being set to 'staging' or 'production' mode, this item is now missing: Screen Shot 2022-08-23 at 10 33 06 AM

Steps to reproduce

  1. Enable the debug toolbar on the front end for the logged-in user.
  2. Set devMode config to false
  3. Load a front end template

Expected behavior

Debug toolbar shows a "db" item.

Actual behavior

Debug toolbar is missing "db" item.

Craft CMS version

4.2.1.1

PHP version

8.1.1

Operating system and version

Darwin 21.3.0

Database type and version

MySQL 5.7.34

Image driver and version

GD 8.1.1

Installed plugins and versions

"craftcms/aws-s3": "2.0.1", "craftcms/feed-me": "5.0.4", "craftcms/redactor": "3.0.2", "doublesecretagency/craft-cpcss": "2.5.0", "fortrabbit/craft-copy": "^2.1", "internetztube/craft-element-relations": "2.0.0", "mmikkel/retcon": "2.4.3", "nystudio107/craft-imageoptimize": "4.0.2", "nystudio107/craft-minify": "4.0.0-beta.2", "nystudio107/craft-seomatic": "4.0.7", "percipioglobal/craft-colour-swatches": "4.2.0.1", "spicyweb/craft-neo": "3.3.4", "verbb/field-manager": "3.0.2", "vlucas/phpdotenv": "^5.4.0", "weareferal/matrix-field-preview": "4.0.2"

brandonkelly commented 2 years ago

That’s because database profiling is only enabled by default when Dev Mode is enabled, as there is a performance cost to it. You can override that behavior by adding this to config/app.php:

return [
    'components' => [
        // ...
        'db' => function() {
            $config = craft\helpers\App::dbConfig();

            // Enable profiling for the debug toolbar
            $config['enableProfiling'] = true;

            return Craft::createObject($config);
        },
    ],
];
turnstylerj commented 2 years ago

Thanks! Just updating for anyone arriving here by a search–given there's a performance cost, I modified this a little to check for a DB_PROFILING env variable so that can be added in development environments only:

return [
    'components' => [
        // ...
        'db' => function() {
            $config = craft\helpers\App::dbConfig();

            // Enable profiling for the debug toolbar
            $config['enableProfiling'] = App::parseBooleanEnv('$DB_PROFILING') ?: false;

            return Craft::createObject($config);
        },
    ],
];

I use this feature a lot in development environments set to "stage" or "production" to test template caching, so hopefully useful to others as well.

nicholashamilton commented 2 years ago

@brandonkelly I've been trying to get the db profiling to show up in the debug toolbar on CraftCMS version "4.2.7". with CRAFT_ENVIRONMENT set to production. No luck on my end using the code snippet you provided. Any idea what may be going on?

brandonkelly commented 2 years ago

@nicholashamilton The code snippet above (for config/app.php) would only work if you have a DB_PROFILING=1 environment variable set. Did you add that?