How to trace or log from within an extension

Discussion in 'Extensions' started by Ernesto, Aug 10, 2017.

Tags:
  1. Ernesto

    Ernesto Member

    Joined:
    Dec 22, 2016
    Messages:
    31
    Likes Received:
    2
    S.E:
    Expired
    L.T:
    Extended
    L.C:
    3
    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?
     
  2. Dev1@OptiBiz1

    Dev1@OptiBiz1 Member

    Joined:
    Feb 17, 2016
    Messages:
    41
    Likes Received:
    10
    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(
                                                
    => '_GET',
                                                
    => '_POST',
                                                
    => '_FILES',
                                                
    => '_SESSION',
                                            ),
                                    ),
                                
    'levels' => 'info, warning, error',
                            ),
                        ),
                    ),
                ),
            ),
    );
    Just make sure you're using the proper logging categories.

    Yes, if you're using the `CFileLogRoute`
     
    twisted1919 likes this.
  3. Ernesto

    Ernesto Member

    Joined:
    Dec 22, 2016
    Messages:
    31
    Likes Received:
    2
    S.E:
    Expired
    L.T:
    Extended
    L.C:
    3
    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.
     
  4. Dev1@OptiBiz1

    Dev1@OptiBiz1 Member

    Joined:
    Feb 17, 2016
    Messages:
    41
    Likes Received:
    10
    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.*'
    ),
     
    twisted1919 likes this.

Share This Page