Hi, Bizley! Thanks for your answer. Can you explain something? traceLevel is not property of Target class, it is property of Logger class. How exactly i can set traceLevel to 0 in my Target class?
Thank you for your solution. I’ve checked this case. Method init() of all Target classes executed when component Dispatcher in the initialization. This happens only once throughout the application life.
The following sample code:
\Yii::error('test', 'catalog-add'); // traceLevel = 0 (conformity to the requirements)
\Yii::error('test'); // ignored 'traceLevel' => 6 in config file, because traceLevel was set to 0 in init method
Bizley, thanks for your support, I solved my problem in this way:
I created ExtendedFileTarget class that extends FileTarget. In the class I overrided method formatMessage from Target class and added traceLevel as property of class.
class ExtendedFileTarget extends FileTarget
{
public $traceLevel = 0;
public function formatMessage($message)
{
list($text, $level, $category, $timestamp) = $message;
$level = Logger::getLevelName($level);
if (!is_string($text)) {
if ($text instanceof \Exception) {
$text = (string) $text;
} else {
$text = VarDumper::export($text);
}
}
$traces = [];
if (isset($message[4]) && $this->traceLevel > 0) {
$count = 0;
foreach ($message[4] as $trace) {
$traces[] = "in {$trace['file']}:{$trace['line']}";
if (++$count >= $this->traceLevel) {
break;
}
}
}
$prefix = $this->getMessagePrefix($message);
return date('Y-m-d H:i:s', $timestamp) . " {$prefix}[$level][$category] $text"
. (empty($traces) ? '' : "\n " . implode("\n ", $traces));
}
}
With this code I can configurate my log.php file as like that:
This case has only one problem: I can’t set value of traceLevel in Target more than traceLevel in Logger class. It means, that value of traceLevel from Logger class is maximum for all targets. But this situation satisfy me in this task
Only problem here might be with the performance - logger gathers all tracelevels first in every case and then it is limited when sending to target #0 but I understand you balance it properly.