/** * Publishes a GELFMessage, returns false if an error occured during write * * @throws UnexpectedValueException * @param unknown_type $message * @return boolean */ public function publish(GELFMessage $message) { // Check if required message parameters are set if (!$message->getShortMessage() || !$message->getHost()) { throw new UnexpectedValueException('Missing required data parameter: "version", "short_message" and "host" are required.'); } // Set Graylog protocol version $message->setVersion(self::GRAYLOG2_PROTOCOL_VERSION); // Encode the message as json string and compress it using gzip $preparedMessage = $this->getPreparedMessage($message); // Open a udp connection to graylog server $socket = $this->getSocketConnection(); // Several udp writes are required to publish the message if ($this->isMessageSizeGreaterChunkSize($preparedMessage)) { // A unique id which consists of the microtime and a random value $messageId = $this->getMessageId(); // Split the message into chunks $messageChunks = $this->getMessageChunks($preparedMessage); $messageChunksCount = count($messageChunks); // Send chunks to graylog server foreach (array_values($messageChunks) as $messageChunkIndex => $messageChunk) { $bytesWritten = $this->writeMessageChunkToSocket($socket, $messageId, $messageChunk, $messageChunkIndex, $messageChunksCount); if (false === $bytesWritten) { // Abort due to write error return false; } } } else { // A single write is enough to get the message published if (false === $this->writeMessageToSocket($socket, $preparedMessage)) { // Abort due to write error return false; } } // This increases stability a lot if messages are sent in a loop // A value of 20 means 0.02 ms usleep(20); // Message successful sent return true; }