ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
CubeHandler.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog\Handler;
13
15
23{
24 private $udpConnection = null;
25 private $httpConnection = null;
26 private $scheme = null;
27 private $host = null;
28 private $port = null;
29 private $acceptedSchemes = array('http', 'udp');
30
38 public function __construct($url, $level = Logger::DEBUG, $bubble = true)
39 {
40 $urlInfos = parse_url($url);
41
42 if (!isset($urlInfos['scheme']) || !isset($urlInfos['host']) || !isset($urlInfos['port'])) {
43 throw new \UnexpectedValueException('URL "'.$url.'" is not valid');
44 }
45
46 if (!in_array($urlInfos['scheme'], $this->acceptedSchemes)) {
47 throw new \UnexpectedValueException(
48 'Invalid protocol (' . $urlInfos['scheme'] . ').'
49 . ' Valid options are ' . implode(', ', $this->acceptedSchemes));
50 }
51
52 $this->scheme = $urlInfos['scheme'];
53 $this->host = $urlInfos['host'];
54 $this->port = $urlInfos['port'];
55
56 parent::__construct($level, $bubble);
57 }
58
64 protected function connectUdp()
65 {
66 if (!extension_loaded('sockets')) {
67 throw new MissingExtensionException('The sockets extension is required to use udp URLs with the CubeHandler');
68 }
69
70 $this->udpConnection = socket_create(AF_INET, SOCK_DGRAM, 0);
71 if (!$this->udpConnection) {
72 throw new \LogicException('Unable to create a socket');
73 }
74
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);
77 }
78 }
79
83 protected function connectHttp()
84 {
85 if (!extension_loaded('curl')) {
86 throw new \LogicException('The curl extension is needed to use http URLs with the CubeHandler');
87 }
88
89 $this->httpConnection = curl_init('http://'.$this->host.':'.$this->port.'/1.0/event/put');
90
91 if (!$this->httpConnection) {
92 throw new \LogicException('Unable to connect to ' . $this->host . ':' . $this->port);
93 }
94
95 curl_setopt($this->httpConnection, CURLOPT_CUSTOMREQUEST, "POST");
96 curl_setopt($this->httpConnection, CURLOPT_RETURNTRANSFER, true);
97 }
98
102 protected function write(array $record)
103 {
104 $date = $record['datetime'];
105
106 $data = array('time' => $date->format('Y-m-d\TH:i:s.uO'));
107 unset($record['datetime']);
108
109 if (isset($record['context']['type'])) {
110 $data['type'] = $record['context']['type'];
111 unset($record['context']['type']);
112 } else {
113 $data['type'] = $record['channel'];
114 }
115
116 $data['data'] = $record['context'];
117 $data['data']['level'] = $record['level'];
118
119 if ($this->scheme === 'http') {
120 $this->writeHttp(json_encode($data));
121 } else {
122 $this->writeUdp(json_encode($data));
123 }
124 }
125
126 private function writeUdp($data)
127 {
128 if (!$this->udpConnection) {
129 $this->connectUdp();
130 }
131
132 socket_send($this->udpConnection, $data, strlen($data), 0);
133 }
134
135 private function writeHttp($data)
136 {
137 if (!$this->httpConnection) {
138 $this->connectHttp();
139 }
140
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.']'))
145 );
146
147 Curl\Util::execute($ch, 5, false);
148 }
149}
Base Handler class providing the Handler structure.
write(array $record)
{Writes the record down to the log of the implementing handler.void}
__construct($url, $level=Logger::DEBUG, $bubble=true)
Create a Cube handler.
Definition: CubeHandler.php:38
connectUdp()
Establish a connection to an UDP socket.
Definition: CubeHandler.php:64
connectHttp()
Establish a connection to a http server.
Definition: CubeHandler.php:83
static execute($ch, $retries=5, $closeAfterDone=true)
Executes a CURL request with optional retries and exception on failure.
Definition: Util.php:32
Exception can be thrown if an extension for an handler is missing.
Monolog log channel.
Definition: Logger.php:28
const DEBUG
Detailed debug information.
Definition: Logger.php:32
$data
$url
Definition: shib_logout.php:72