How to trace or log from within an extension

Ernesto

Member
Hi, I want to Yii::trace() or Yii::log() from within an extension, how would I do that?
Should I add something to apps/common/config/main.php?
Should the logged lines appear in apps/common/runtime?
 
Should I add something to apps/common/config/main.php?
No, you only need to use main-custom.php

main-custom.php
PHP:
return array(
    'components' =>
        array(
            'db' =>
                array(
                    'connectionString' => 'mysql:host=localhost;dbname=mailwizz',
                    'username' => 'root',
                    'password' => '',
                    'tablePrefix' => 'mw_',
                ),


            // THIS component
            'log' => array(
                'routes' => array(
                    array(
                        // This route will store info, warning, and error to application.log
                        array(
                            'class' => 'CFileLogRoute',
                            'filter' => 'CLogFilter',
                            'levels' => 'info, warning, error',
                        ),
                        // This route will store info, warning, and error to your-extension.log
                        array(
                            'class' => 'CFileLogRoute',
                            'logFile' => 'your-extension.log',
                            'filter' => 'CLogFilter',
                            'levels' => 'info, warning, error',
                        ),
                        // This route will store info, warning, error to db table application_log
                        array(
                            'class' => 'CDbLogRoute',
                            'connectionID' => 'db',
                            'logTableName' => '{{application_log}}',
                            'filter' =>
                                array(
                                    'class' => 'CLogFilter',
                                    'logUser' => false,
                                    'logVars' =>
                                        array(
                                            0 => '_GET',
                                            1 => '_POST',
                                            2 => '_FILES',
                                            3 => '_SESSION',
                                        ),
                                ),
                            'levels' => 'info, warning, error',
                        ),
                    ),
                ),
            ),
        ),
);

Just make sure you're using the proper logging categories.

Should the logged lines appear in apps/common/runtime?
Yes, if you're using the `CFileLogRoute`
 
And what categories should I use? I know this is a question about Yii and not so much about Mailwizz, but I never found this to be clear in Yii's documentation.
 
I never found this to be clear in Yii's documentation.
That is because the categories will be up to you. In my extensions, I always use the format "ext.you-extension-name.appName". So it will look like this in the code
PHP:
Yii::log("Something went wrong", CLogger::ERROR, "ext.your-extension-name.backend");

So in main-custom.php, I can just specify the category as
PHP:
// This route will store info, warning, and error to your-extension.log
array(
    'class' => 'CFileLogRoute',
    'logFile' => 'your-extension.log',
    'filter' => 'CLogFilter',
    'levels' => 'info, warning, error',
    // this log route will capture all categories that starts with ext.
    'categories' => 'ext.*'
),
 
Back
Top