2

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
  • 1
    Sounds like you have devMode on? Aug 13, 2020 at 20:26

2 Answers 2

0

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

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

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.

If you only want to remove the $_SERVER variables from being logged I recommend changing it for the existing Target classes that Craft uses for logging. This could be done as follows:

foreach(Craft::$app->getLog()->targets as $target) {
   $target->logVars = array_diff($target->logVars, ['_SERVER']);
}

If you want to exclude classes which logs all mysql queries, you can also specify class exceptions. e.g.: 'except' => [ 'yii\db\Connection::*' ],

In my case we redefined the whole log function for CraftCMS. Mainly it does :

  • redirect output to php://stderr
  • exclude certain classes from logging
  • remove all variables like $_SERVER, $_GET

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\base\View::renderFile',
                            'yii\db\Command::*',
                            '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.

5
  • 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
    – owenmelbz
    May 10, 2021 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, 2021 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, 2021 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?
    – owenmelbz
    May 10, 2021 at 13:19
  • True. I adapted first half of my post to show you how you could modify existing Craft objects instead of redefining the Target class. Needs some testing though. But for all this I recommend you read the code yii2\log\Target and maybe subclasses you need in your case. As this class is really the origin where most of the logging logic happens.
    – judos
    Jul 1, 2021 at 9:58

Your Answer

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

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