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 :)
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIf you disable devMode in config/general.php
, that information won't be logged.
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 :
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.
'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.
devMode
on?