/**
  * send a message to specified fluentd.
  *
  * @todo use HTTP1.1 protocol and persistent socket.
  * @param string $tag
  * @param array  $data
  */
 public function post2(Entity $entity)
 {
     $packed = json_encode($entity->getData());
     $request = sprintf('http://%s:%d/%s?json=%s', $this->host, $this->port, $entity->getTag(), urlencode($packed));
     $ret = file_get_contents($request);
     return $ret !== false;
 }
 /**
  * @param Entity $entity
  * @return int
  */
 protected function postImpl(Entity $entity)
 {
     /*
      * example ouputs:
      *   2012-02-26T01:26:20+0900        debug.test      {"hello":"world"}
      */
     $format = "%s\t%s\t%s\n";
     return $this->write(sprintf($format, date(\DateTime::ISO8601, $entity->getTime()), $entity->getTag(), json_encode($entity->getData())));
 }
 protected function postImpl(Entity $entity)
 {
     $packed = json_encode($entity->getData());
     $data = $wbuffer = sprintf("%s\t%s\t%s", date(\DateTime::ISO8601, $entity->getTime()), $entity->getTag(), $packed . PHP_EOL);
     $length = strlen($data);
     $written = 0;
     $retry = 0;
     try {
         if (!flock($this->fp, LOCK_EX)) {
             throw new \Exception('could not obtain LOCK_EX');
         }
         fseek($this->fp, 0, SEEK_END);
         while ($written < $length) {
             $nwrite = fwrite($this->fp, $wbuffer);
             if ($nwrite === false) {
                 throw new \Exception("could not write message");
             } else {
                 if ($nwrite === "") {
                     throw new \Exception("connection aborted");
                 } else {
                     if ($nwrite === 0) {
                         if ($retry > self::MAX_WRITE_RETRY) {
                             throw new \Exception("failed fwrite retry: max retry count");
                         }
                         $retry++;
                     }
                 }
             }
             $written += $nwrite;
             $wbuffer = substr($wbuffer, $written);
         }
         flock($this->fp, LOCK_UN);
     } catch (\Exception $e) {
         $this->processError($this, $entity, $e->getMessage());
         return false;
     }
     return true;
 }
 public function defaultErrorHandler(BaseLogger $logger, Entity $entity, $error)
 {
     error_log(sprintf("ChainLogger: %s %s %s: %s", get_class($logger), $error, $entity->getTag(), json_encode($entity->getData())));
 }
 /**
  * pack entity as a json string.
  *
  * @param Entity $entity
  * @return string
  */
 public function pack(Entity $entity)
 {
     return json_encode(array($entity->getTag(), $entity->getTime(), $entity->getData()));
 }