Thursday, January 27, 2022

[FIXED] Yii separate log files for each user

Issue

I want to store my log messages in separate log files for each user. My current config is like:

'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
    array(
        'class'=>'CFileLogRoute',
        'logFile'=>'custom.log.u1',
        'categories'=>'error.*',
    ),
    array(
        'class'=>'CFileLogRoute',
        'logFile'=>'custom.log.u2',
        'categories'=>'error.*',
    ),
    ...
))

but my user are more than 2 and the log file name should set dynamically ... how can i do that ... ?


Solution

The I can think of is to extend CFileLogRoute and override some of the functions:

make a php file named MyLog in a folder that gets autoloaded like components

class MyLog extends CFileLogRoute
{
    public $filderName;

    public function getLogFile()
    {
        return parent::getLogFile() . (int)Yii::app()->user->id;
    }

    public function setLogFile($value)
    {
        parent::setLogFile($value . (int)Yii::app()->user->id);
    }
}

then in your config:

'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
    array(
        'class'=>'MyLog',
        'categories'=>'error.*',
    ),
    ...
))


Answered By - Developerium

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.