コード例 #1
0
ファイル: AuditLog.php プロジェクト: recipe/scalr
 /**
  * {@inheritdoc}
  * @see Scalr\Logger.AuditLogInterface::log()
  */
 public function log($message, $tags, $object = null, $objectBefore = null)
 {
     if (!$this->isEnabled()) {
         return true;
     }
     $user = $this->getUser();
     if (!$this->getContainer()->initialized('request') || !$this->getContainer()->request instanceof \Scalr_UI_Request) {
         $ip = '127.0.0.1';
         $envid = 0;
     } else {
         /* @var $request \Scalr_UI_Request */
         $request = $this->getContainer()->request;
         $ip = $request->getClientIp();
         if ($request->getEnvironment() === null) {
             $envid = 0;
         } else {
             $envid = $request->getEnvironment()->id;
         }
     }
     if (!$tags instanceof AuditLogTags) {
         $tags = new AuditLogTags(!empty($tags) && is_array($tags) ? $tags : null);
     }
     $record = new LogRecord();
     $record->setEnvid($envid)->setUserid($user->getId())->setEmail($user->getEmail())->setAccountid($user->getAccountId())->setIp(ip2long($ip))->setTime(new \DateTime(null, new \DateTimeZone('UTC')))->setMessage($message)->setTags($tags);
     if ($object !== null || $objectBefore !== null) {
         $classes = array();
         if ($object !== null) {
             if (!$object instanceof AbstractAuditLogDocument) {
                 $object = $this->getObjectDocument($object);
             }
             $classes[0] = get_class($object);
         }
         if ($objectBefore !== null) {
             if (!$objectBefore instanceof AbstractAuditLogDocument) {
                 $objectBefore = $this->getObjectDocument($objectBefore);
             }
             $classes[1] = get_class($objectBefore);
         }
         if (count($classes) != 2) {
             //Empty state has been before or will be after the change.
             list($index, $dataClass) = each($classes);
             if ($index == 0) {
                 $objectBefore = new $dataClass();
             } else {
                 $object = new $dataClass();
             }
         }
         $keyValueRecord = $this->getKeyValueRecord($objectBefore, $object);
         if ($keyValueRecord == null) {
             //No changes found.
             return true;
         }
         $record->setData($keyValueRecord);
     }
     try {
         $res = $this->storage->write($record);
     } catch (\Exception $e) {
         error_log(sprintf('AuditLog::log() failed. %s %s.', get_class($e), $e->getMessage()));
         $res = true;
     }
     return $res;
 }