yii\log\Logger::log PHP Method

log() public method

If [[traceLevel]] is greater than 0, additional call stack information about the application code will be logged as well.
public log ( string | array $message, integer $level, string $category = 'application' )
$message string | array the message to be logged. This can be a simple string or a more complex data structure that will be handled by a [[Target|log target]].
$level integer the level of the message. This must be one of the following: `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`.
$category string the category of the message.
    public function log($message, $level, $category = 'application')
    {
        $time = microtime(true);
        $traces = [];
        if ($this->traceLevel > 0) {
            $count = 0;
            $ts = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
            array_pop($ts);
            // remove the last trace since it would be the entry script, not very useful
            foreach ($ts as $trace) {
                if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII2_PATH) !== 0) {
                    unset($trace['object'], $trace['args']);
                    $traces[] = $trace;
                    if (++$count >= $this->traceLevel) {
                        break;
                    }
                }
            }
        }
        $this->messages[] = [$message, $level, $category, $time, $traces];
        if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) {
            $this->flush();
        }
    }

Usage Example

Beispiel #1
0
 public function testLog()
 {
     $logger = new Logger();
     $logger->log('test1', Logger::LEVEL_INFO);
     $this->assertEquals(1, count($logger->messages));
     $this->assertEquals('test1', $logger->messages[0][0]);
     $this->assertEquals(Logger::LEVEL_INFO, $logger->messages[0][1]);
     $this->assertEquals('application', $logger->messages[0][2]);
     $logger->log('test2', Logger::LEVEL_ERROR, 'category');
     $this->assertEquals(2, count($logger->messages));
     $this->assertEquals('test2', $logger->messages[1][0]);
     $this->assertEquals(Logger::LEVEL_ERROR, $logger->messages[1][1]);
     $this->assertEquals('category', $logger->messages[1][2]);
 }
All Usage Examples Of yii\log\Logger::log