1

Yii how can i see, which part of code logging a string in Yii log file ?

RusAlex
  • 8,245
  • 6
  • 36
  • 44

3 Answers3

1

I have not tried it but you can update or extend CLogger->log() method to add some backtrace information to log message, something like,

public function log($message,$level='info',$category='application')
{
       $e = new Exception;
       $message .= $e->getTraceAsString();

       $this->_logs[]=array($message,$level,$category,microtime(true));
       $this->_logCount++;
       if($this->autoFlush>0 && $this->_logCount>=$this->autoFlush && !$this->_processing)
       {
           $this->_processing=true;
           $this->flush($this->autoDump);
           $this->_processing=false;
       }
}

So now each log line contains a back trace.

for additional info about getting 'backtrace' or 'callstack' read this post - Print PHP Call Stack
Please correct me if something wrong here.

Community
  • 1
  • 1
Uday Sawant
  • 5,748
  • 3
  • 32
  • 45
1

It's in the index.php (or whichever entry file you're using) add the line:

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',2);

to the file before the create application call.

Change the number at the end for the number of lines from the stack trace you want.

Only real limitation is it appears on every log message, there's no way to filter.

Paystey
  • 3,287
  • 2
  • 18
  • 32
0

I'm not sure I understand you question, but the trace log (protected/runtime/trace.log) already indicates the filename (line #) where the Yii::app()->trace() call is located, like this example log record:

2012/01/11 12:54:28 [trace] [system.db.CDbCommand] Querying SQL: SELECT * FROM `folks` `f` WHERE `f`.`idfolks` IS NULL LIMIT 1
in /home/hobs/src/appname/protected/controllers/SiteController.php (970)
in /home/hobs/src/appname/index.php (14)

Assuming you've set up your trace log router in protected/config/main.php...

'log'=>array(
  'class'=>'CLogRouter',
  'routes'=>array(
    array(
      'class'=>'CFileLogRoute',
      'levels'=>'trace', //, info, error, warning', // empty means all levels = default
      //'categories'=>'',//'application',// system.*', // default = empty = all categories
      'logFile'=>'trace.log',
    ),
    ...
hobs
  • 18,473
  • 10
  • 83
  • 106