TransWikia.com

Is it possible to stop Craft from logging so much?

Craft CMS Asked on September 4, 2021

I’ve got a Craft CMS installation that is set to production mode. I notice that my logs get flooded with a bunch of entries which don’t seem very useful.

For example, in a matter of several days, I’ve already got console.log up to console.log.5 – So it’s rotated the log file 5 times already. These files are filled with what seems like every single query that gets executed on the website. They’re 70k lines a piece. It also logs a lot of entries in queue.log, among other files.

How can I stop logging information and only log things which require my attention, such as errors?

Thanks!

4 Answers

Even so this thread is old, maybe my answer can help someone: Cleanup Craft Logs

Basically it's possible via app.php to remove all unnecessary logs such as all the $_GET, $_POST, $_SERVER variables and all the SQL being logged.

The main point you need is to configure in app.php the log component. The entry 'logVars' will get rid of all the $_SERVER values logged (also fixed security issues with password appearing in the logs). The other interesting configuration is the 'except' array where we excluded a few classes. 'yiidbConnection::*' for example removes all SQL logs.

return [
    'modules' => [
        'mymodule' => MyModule::class,
        // My modules mapped here
    ],
    'components' => [
        'log' => function() {
            return Craft::createObject([
                'class' => yiilogDispatcher::class,
                'targets' => [
                    [
                        'class' => codemixstreamlogTarget::class,
                        'url' => 'php://stderr',
                        'levels' => Craft::$app->getConfig()->general->devMode ?
                            (yiilogLogger::LEVEL_ERROR | yiilogLogger::LEVEL_WARNING | yiilogLogger::LEVEL_INFO) :
                            (yiilogLogger::LEVEL_ERROR | yiilogLogger::LEVEL_WARNING),
                        'except' => [
                            'yiibaseView::renderFile',
                            'yiidbCommand::*',
                            'yiidbConnection::*' // We don't want sql to be logged
                        ],
                        // don't log global variables like $_SERVER etc periodically in logs.
                        // See: yiilogTarget::class, collect method usually appends getContextMessage
                        'logVars' => []
                    ]
                ]
            ]);
        }
    ],
    'bootstrap' => [
        // My module names here
    ],
];

Answered by judos on September 4, 2021

We were having the same issue. Our logs were getting so dense with SQL statements that it was hard to find our errors. And for the most part, seeing Craft's internal SQL would not help us debug the problem.

What we've found is we can use the onelogger plugin. After installing it, we just needed to add a file called onelogger.php to our config file. This is what we used:

<?php

return [
  'logLevels' => getenv('ONELOGGER_LOG_LEVELS') ? explode(',', getenv('ONELOGGER_LOG_LEVELS')) : ['warning', 'error']
];

Answered by Francis McGrath on September 4, 2021

As someone who theoretically might inherit your site one day... and also as future-you trying to diagnose a problem... please don't try to limit what Craft logs. If you want a quicker way to look for critical problems, check out the Yii2 Debug Toolbar as discussed here Profiling your Website with Craft CMS 3’s Debug Toolbar

You can use it to very quickly and easily see only errors, or only warnings, and so on. And then if the additional data that Craft logs is needed to help you diagnose a problem, it'll still be there.

To learn how to parse the Craft CMS log files quickly, check out the Zen and the Art of Craft CMS Log File Reading

If you do end up wanting to make your own logger, check out the Creating a Custom Logger for Craft CMS article

I echo Brad's comments, though, unless for some reason devMode is on, it shouldn't be logging every query, except for performance profiling.

Answered by andrew.welch on September 4, 2021

This will be a similar answer as here: Craft 3 large log file sizes

Basically Craft goes with Yii's default FileTarget settings, which is 5 log files of 10MB each before they start rotating out. Those are individual limits for both web.log and console.log files.

If you want to change those behaviors you'll need to modify those properties via a config/app.php file using your own FileTarget settings.

There's another post here with some useful guidance: Craft 3 plugins - Logging in a separate *.log file

Answered by Brad Bell on September 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP