Message Log

Hi Dear,

How can i change formate message in cLogRoute, I want add Ip address and username and change date format,

format date in database is second, i want save local time in database,

I create separe class file in protext/extension




class LogDb   extends CDbLogRoute

{

    protected function formatLogMessage($message, $level, $category, $time)

    {

//        if ($time === null)

//            $time = time();


          $message= $_SERVER['REMOTE_ADDR']. Yii::app()->user->name.$message;


//        return date('Y/m/d H:i:s',$time)." [$level] [$category] $message\n";


        return @date( 'M d H:i:s') . ' [' . sprintf( '%-30s', $category ) . '] ' . ': <' . $level . '> ' . $message . PHP_EOL;

    }

}



and in main.php




            'log'=>array(

                'class'=>'CLogRouter',

                'routes'=>array(

                    array(

                        'class'=>'CDbLogRoute',

                    )   ,

                    array(

                        'class'=>'application.extensions.LogDb',

                        'autoCreateLogTable'=>true,

                        'connectionID'=>'db',

//                        'logTableName'=>'log_yii2',

//                        'enabled'=>true,

                        'levels'=>'error, warning',

//                        'logFile' => 'Log',

                    ),



but message in database not changed!!!

tanks for attension

Why don’t you remove at least the ‘old’ logroute? In my opinion you would trigger two routes by this.




 array(

                        'class'=>'CDbLogRoute',

                    )   ,



Finally I found solution :D

I must change the method processLogs




protected function processLogs($logs)

    {

        $command=$this->getDbConnection()->createCommand();

        $message='User: '.Yii::app()->user->name.' From IP: '.$_SERVER['REMOTE_ADDR'] ;

        $logTime=date('Y-m-d H:i:s');

        foreach($logs as $log)

        {

            $command->insert($this->logTableName,array(

                'level'=>$log[1],

                'category'=>$log[2],

                'logtime'=>$logTime,

                'message'=>$message.' '.$log[0]

            ));

        }

    }



Yii Is Great FrameWork,

Thanks