40 $urlInfos = parse_url(
$url);
42 if (!isset($urlInfos[
'scheme']) || !isset($urlInfos[
'host']) || !isset($urlInfos[
'port'])) {
43 throw new \UnexpectedValueException(
'URL "'.
$url.
'" is not valid');
46 if (!in_array($urlInfos[
'scheme'], $this->acceptedSchemes)) {
47 throw new \UnexpectedValueException(
48 'Invalid protocol (' . $urlInfos[
'scheme'] .
').' 49 .
' Valid options are ' . implode(
', ', $this->acceptedSchemes));
52 $this->scheme = $urlInfos[
'scheme'];
53 $this->host = $urlInfos[
'host'];
54 $this->port = $urlInfos[
'port'];
66 if (!extension_loaded(
'sockets')) {
70 $this->udpConnection = socket_create(AF_INET, SOCK_DGRAM, 0);
71 if (!$this->udpConnection) {
72 throw new \LogicException(
'Unable to create a socket');
75 if (!socket_connect($this->udpConnection, $this->host, $this->port)) {
76 throw new \LogicException(
'Unable to connect to the socket at ' . $this->host .
':' . $this->port);
85 if (!extension_loaded(
'curl')) {
86 throw new \LogicException(
'The curl extension is needed to use http URLs with the CubeHandler');
89 $this->httpConnection = curl_init(
'http://'.$this->host.
':'.$this->port.
'/1.0/event/put');
91 if (!$this->httpConnection) {
92 throw new \LogicException(
'Unable to connect to ' . $this->host .
':' . $this->port);
95 curl_setopt($this->httpConnection, CURLOPT_CUSTOMREQUEST,
"POST");
96 curl_setopt($this->httpConnection, CURLOPT_RETURNTRANSFER,
true);
102 protected function write(array $record)
104 $date = $record[
'datetime'];
106 $data = array(
'time' => $date->format(
'Y-m-d\TH:i:s.uO'));
107 unset($record[
'datetime']);
109 if (isset($record[
'context'][
'type'])) {
110 $data[
'type'] = $record[
'context'][
'type'];
111 unset($record[
'context'][
'type']);
113 $data[
'type'] = $record[
'channel'];
116 $data[
'data'] = $record[
'context'];
117 $data[
'data'][
'level'] = $record[
'level'];
119 if ($this->scheme ===
'http') {
128 if (!$this->udpConnection) {
132 socket_send($this->udpConnection,
$data, strlen(
$data), 0);
137 if (!$this->httpConnection) {
141 curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS,
'['.
$data.
']');
142 curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, array(
143 'Content-Type: application/json',
144 'Content-Length: ' . strlen(
'['.
$data.
']'))
const DEBUG
Detailed debug information.
Base Handler class providing the Handler structure.
static execute($ch, $retries=5, $closeAfterDone=true)
Executes a CURL request with optional retries and exception on failure.
connectHttp()
Establish a connection to a http server.
__construct($url, $level=Logger::DEBUG, $bubble=true)
Create a Cube handler.
Exception can be thrown if an extension for an handler is missing.
connectUdp()
Establish a connection to an UDP socket.