Beispiel #1
0
 /**
  * @param string $message
  * @param int $code
  * @param \Exception|int $logLevel
  * @param \Exception|null $previous
  */
 public function __construct($message = "", $code = 0, $logLevel = LogLevel::ERROR, \Exception $previous = null)
 {
     parent::__construct($message, $code, $previous);
     if (LogLevel::isValidLevel($logLevel)) {
         $this->logLevel = $logLevel;
     } else {
         $this->logLevel = LogLevel::ERROR;
     }
 }
 /**
  * Writes the log record
  *
  * @param \TYPO3\CMS\Core\Log\LogRecord $record Log record
  * @return \TYPO3\CMS\Core\Log\Writer\WriterInterface $this
  * @throws \RuntimeException
  */
 public function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record)
 {
     $levelName = \TYPO3\CMS\Core\Log\LogLevel::getName($record->getLevel());
     $data = $record->getData();
     $data = !empty($data) ? '- ' . json_encode($data) : '';
     $message = sprintf('TYPO3 [%s] request="%s" component="%s": %s %s', $levelName, $record->getRequestId(), $record->getComponent(), $record->getMessage(), $data);
     if (FALSE === error_log($message)) {
         throw new \RuntimeException('Could not write log record to PHP error log', 1345036336);
     }
     return $this;
 }
 /**
  * Writes the log record
  *
  * @param LogRecord $record Log record
  * @return \TYPO3\CMS\Core\Log\Writer\WriterInterface $this
  * @throws \RuntimeException
  */
 public function writeLog(LogRecord $record)
 {
     $levelName = LogLevel::getName($record->getLevel());
     $data = '';
     $recordData = $record->getData();
     if (!empty($recordData)) {
         // According to PSR3 the exception-key may hold an \Exception
         // Since json_encode() does not encode an exception, we run the _toString() here
         if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
             $recordData['exception'] = (string) $recordData['exception'];
         }
         $data = '- ' . json_encode($recordData);
     }
     $message = sprintf('TYPO3 [%s] request="%s" component="%s": %s %s', $levelName, $record->getRequestId(), $record->getComponent(), $record->getMessage(), $data);
     if (FALSE === error_log($message)) {
         throw new \RuntimeException('Could not write log record to PHP error log', 1345036336);
     }
     return $this;
 }
Beispiel #4
0
 /**
  * Writes the log record
  *
  * @param LogRecord $record Log record
  * @return WriterInterface $this
  * @throws \RuntimeException
  */
 public function writeLog(LogRecord $record)
 {
     $timestamp = date('r', (int) $record->getCreated());
     $levelName = LogLevel::getName($record->getLevel());
     $data = '';
     $recordData = $record->getData();
     if (!empty($recordData)) {
         // According to PSR3 the exception-key may hold an \Exception
         // Since json_encode() does not encode an exception, we run the _toString() here
         if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
             $recordData['exception'] = (string) $recordData['exception'];
         }
         $data = '- ' . print_r($recordData, true);
     }
     $message = sprintf('%s [%s] request="%s" component="%s": %s %s', $timestamp, $levelName, $record->getRequestId(), $record->getComponent(), $record->getMessage(), $data);
     if (false === fwrite(self::$logFileHandles[$this->logFile], $message . LF)) {
         throw new \RuntimeException('Could not write log record to log file', 1345036335);
     }
     return $this;
 }
Beispiel #5
0
 /**
  * Adds a log record.
  *
  * @param integer $level Log level.
  * @param string $message Log message.
  * @param array $data Additional data to log
  * @return mixed
  */
 public function log($level, $message, array $data = array())
 {
     \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
     if ($level > $this->minimumLogLevel) {
         return $this;
     }
     /** @var $record \TYPO3\CMS\Core\Log\LogRecord */
     $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', $this->name, $level, $message, $data);
     $record = $this->callProcessors($record);
     $this->writeLog($record);
     return $this;
 }
Beispiel #6
0
 /**
  * Returns the configuration from $TYPO3_CONF_VARS['LOG'] as
  * hierarchical array for different components of the class hierarchy.
  *
  * @param string $configurationType Type of config to return (writer, processor)
  * @param string $loggerName Logger name
  * @throws \RangeException
  * @return array
  */
 protected function getConfigurationForLogger($configurationType, $loggerName)
 {
     // Split up the logger name (dot-separated) into its parts
     $explodedName = explode('.', $loggerName);
     // Search in the $TYPO3_CONF_VARS['LOG'] array
     // for these keys, for example "writerConfiguration"
     $configurationKey = $configurationType . 'Configuration';
     $configuration = $GLOBALS['TYPO3_CONF_VARS']['LOG'];
     $result = $configuration[$configurationKey] ?: array();
     // Walk from general to special (t3lib, t3lib.db, t3lib.db.foo)
     // and search for the most specific configuration
     foreach ($explodedName as $partOfClassName) {
         if (!empty($configuration[$partOfClassName][$configurationKey])) {
             $result = $configuration[$partOfClassName][$configurationKey];
         }
         $configuration = $configuration[$partOfClassName];
     }
     // Validate the config
     foreach ($result as $level => $unused) {
         try {
             \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
         } catch (\RangeException $e) {
             throw new \RangeException('The given severity level "' . htmlspecialchars($level) . '" for ' . $configurationKey . ' of logger "' . $loggerName . '" is not valid.', 1326406447);
         }
     }
     return $result;
 }
 /**
  * @test
  * @dataProvider isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSoDataProvider
  * @expectedException \RangeException
  */
 public function isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSo($inputValue)
 {
     \TYPO3\CMS\Core\Log\LogLevel::validateLevel($inputValue);
 }
Beispiel #8
0
 /**
  * Convert record to string for simple output, like echo().
  * Contents of data array is appended as JSON-encoded string
  *
  * @return string
  */
 public function __toString()
 {
     $timestamp = date('r', (int) $this->created);
     $levelName = \TYPO3\CMS\Core\Log\LogLevel::getName($this->level);
     $data = !empty($this->data) ? '- ' . json_encode($this->data) : '';
     $logRecordString = sprintf('%s [%s] request="%s" component="%s": %s %s', $timestamp, $levelName, $this->requestId, $this->component, $this->message, $data);
     return $logRecordString;
 }
Beispiel #9
0
 /**
  * @test
  */
 public function normalizeLevelDoesNotConvertInvalidLevel()
 {
     $levelString = 'invalid';
     $this->assertEquals($levelString, \TYPO3\CMS\Core\Log\LogLevel::normalizeLevel($levelString));
 }
 /**
  * @test
  * @dataprovider isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSoDataProvider
  */
 public function isValidLevelThrowsExceptionOnInvalidLevelIfAskedToDoSo($inputValue)
 {
     $this->setExpectedException('RangeException');
     \TYPO3\CMS\Core\Log\LogLevel::validateLevel($inputValue);
 }
 /**
  * @param string $property
  * @return integer|NULL
  */
 protected function setLogLevelThresholdByExtensionConfigurationProperty($property)
 {
     if (array_key_exists($property, $this->extensionConfiguration)) {
         LogLevel::validateLevel($this->extensionConfiguration[$property]);
         $this->{$property} = (int) $this->extensionConfiguration[$property];
     }
 }
Beispiel #12
0
 /**
  * Convert record to string for simple output, like echo().
  * Contents of data array is appended as JSON-encoded string
  *
  * @return string
  */
 public function __toString()
 {
     $timestamp = date('r', (int) $this->created);
     $levelName = LogLevel::getName($this->level);
     $data = '';
     if (!empty($this->data)) {
         // According to PSR3 the exception-key may hold an \Exception
         // Since json_encode() does not encode an exception, we run the _toString() here
         if (isset($this->data['exception']) && $this->data['exception'] instanceof \Exception) {
             $this->data['exception'] = (string) $this->data['exception'];
         }
         $data = '- ' . json_encode($this->data);
     }
     $logRecordString = sprintf('%s [%s] request="%s" component="%s": %s %s', $timestamp, $levelName, $this->requestId, $this->component, $this->message, $data);
     return $logRecordString;
 }