How can we disable Craft from logging MySQL queries and $_SERVER object into the logs?

For our team, this creates additional noise that's not needed for us to debug issues.

Thanks :)

  • 1
    Sounds like you have devMode on? – RitterKnight Aug 13 '20 at 20:26

If you disable devMode in config/general.php, that information won't be logged.

  • devMode is turned off, but we're still at a minimum getting $_SERVER each time – Owen May 10 at 8:34

Two links where I posted the result. Initially my own question: https://craftcms.stackexchange.com/a/37040/10980 Is it possible to stop Craft from logging so much?

You need to configure app.php and set the variable 'logVars' => [] This removes all variables like $_SERVER, $_GET etc. from being logged.

Also you want to exclude the class which logs all mysql queries. For this you can specify class exceptions: 'except' => [ 'yii\db\Connection::*' ],

The whole app.php with the above two configs looks as follows for me.

return [
    'modules' => [
        'mymodule' => MyModule::class,
        // My modules mapped here
    'components' => [
        'log' => function() {
            return Craft::createObject([
                'class' => yii\log\Dispatcher::class,
                'targets' => [
                        // You might not need class/url/levels property:
                        'class' => codemix\streamlog\Target::class,
                        'url' => 'php://stderr',
                        'levels' => Craft::$app->getConfig()->general->devMode ?
                            (yii\log\Logger::LEVEL_ERROR | yii\log\Logger::LEVEL_WARNING | yii\log\Logger::LEVEL_INFO) :
                            (yii\log\Logger::LEVEL_ERROR | yii\log\Logger::LEVEL_WARNING),
                        // Remove SQL Queries or any other class which logs:
                        'except' => [
                            'yii\db\Connection::*' // We don't want sql to be logged
                        // don't log global variables like $_SERVER etc periodically in logs.
                        // See: yii\log\Target::class, collect method usually appends getContextMessage
                        'logVars' => []
    'bootstrap' => [
        // My module names here

Hope that helps.

  • devMode doesn't seem to turned off the extra $_SERVER we've got many sites all in production with devMode off and it still logs it out – Owen May 10 at 8:34
  • No I didn't mean to turn on devMode, just that you can easily do it no matter what you configured in devMode. Basically in your app.php you can add some configuration for loggers. In this configuration the main point is to specify 'logVars' => [] You can actually find the code in yii framework that logs those $_SERVER variables in yii\log\Target::class, the getContextMessage method. To see my example configuration of app.php which gives you an impression how it looks with comments see my second link in above answer. – judos May 10 at 9:56
  • Updated my answer to be more specific, even so it's now the same as the links I posted. – judos May 10 at 10:02
  • 1
    Hi, thanks for the more full example - however the problem I think we're having with the provided examples - they do "more" than we ideally want - We want 100% the exact defaults for Craft, but just with $_SERVER removed - from the example you've provided it would seem that it completely overwrites the defaults with a custom implementation? We don't know enough about the internals of Craft to know what we've changed and what knock on effects it will make. As far as you know is there anyway to simply "extend" or "merge" into the config rather than replacing it? – Owen May 10 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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