mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-26 14:34:32 +02:00
PSR-2 fixes and lazy connection
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of the Monolog package.
|
* This file is part of the Monolog package.
|
||||||
*
|
*
|
||||||
* (c) Wan Chen <kami@kamisama.me>
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
@@ -15,136 +15,150 @@ use Monolog\Logger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs to Cube.
|
* Logs to Cube.
|
||||||
* Cube url : http://square.github.com/cube/
|
* @link http://square.github.com/cube/
|
||||||
*
|
|
||||||
* usage example:
|
|
||||||
*
|
|
||||||
* $log = new Logger('application');
|
|
||||||
* // To use UDP
|
|
||||||
* $cube = new CubeHandler("udp://127.0.0.1:1180");
|
|
||||||
* // To use HTTP
|
|
||||||
* $cube = new CubeHandler("http://localhost:1080");
|
|
||||||
* $log->pushHandler($cube);
|
|
||||||
*
|
|
||||||
* Given host and port are cube's default
|
|
||||||
*
|
*
|
||||||
* @author Wan Chen <kami@kamisama.me>
|
* @author Wan Chen <kami@kamisama.me>
|
||||||
*/
|
*/
|
||||||
class CubeHandler extends AbstractProcessingHandler
|
class CubeHandler extends AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
private $socket = null;
|
private $udpConnection = null;
|
||||||
private $curlConnection = null;
|
private $httpConnection = null;
|
||||||
private $urlScheme = null;
|
|
||||||
private $acceptedUrlScheme = array('http', 'udp');
|
private $scheme = null;
|
||||||
|
private $host = null;
|
||||||
|
private $port = null;
|
||||||
/**
|
private $acceptedScheme = array('http', 'udp');
|
||||||
* Create a Cube handler
|
|
||||||
*
|
|
||||||
* @throws UnexpectedValueException when given url is not a valid url.
|
/**
|
||||||
* A valid url must consists of three parts : protocol://host:port
|
* Create a Cube handler
|
||||||
* Only valid protocol used by Cube are http and udp
|
*
|
||||||
*/
|
* @throws UnexpectedValueException when given url is not a valid url.
|
||||||
|
* A valid url must consists of three parts : protocol://host:port
|
||||||
|
* Only valid protocol used by Cube are http and udp
|
||||||
|
*/
|
||||||
public function __construct($url, $level = Logger::DEBUG, $bubble = true)
|
public function __construct($url, $level = Logger::DEBUG, $bubble = true)
|
||||||
{
|
{
|
||||||
$urlInfos = parse_url($url);
|
$urlInfos = parse_url($url);
|
||||||
|
|
||||||
if(!$urlInfos || !isset($urlInfos['scheme']) || !isset($urlInfos['host']) || !isset($urlInfos['port']))
|
if (!$urlInfos || !isset($urlInfos['scheme'])
|
||||||
throw new \UnexpectedValueException('URL "'.$url.'" is not valid');
|
|| !isset($urlInfos['host']) || !isset($urlInfos['port'])) {
|
||||||
|
throw new \UnexpectedValueException('URL "'.$url.'" is not valid');
|
||||||
if (!in_array($urlInfos['scheme'], $this->acceptedUrlScheme))
|
}
|
||||||
{
|
|
||||||
throw new \UnexpectedValueException('Invalid ' . $urlInfos['scheme'] . ' protocol. Valid options are ' . implode(', ', $this->acceptedUrlScheme));
|
if (!in_array($urlInfos['scheme'], $this->acceptedScheme)) {
|
||||||
}
|
throw new \UnexpectedValueException(
|
||||||
else
|
'Invalid ' . $urlInfos['scheme'] . ' protocol.'
|
||||||
$this->urlScheme = $urlInfos['scheme'];
|
. 'Valid options are ' . implode(', ', $this->acceptedScheme));
|
||||||
|
} else {
|
||||||
switch($this->urlScheme)
|
$this->scheme = $urlInfos['scheme'];
|
||||||
{
|
$this->host = $urlInfos['host'];
|
||||||
case 'http' : $this->connectHttp($urlInfos['host'], $urlInfos['port']); break;
|
$this->port = $urlInfos['port'];
|
||||||
case 'udp' : $this->connectUdp($urlInfos['host'], $urlInfos['port']); break;
|
}
|
||||||
default;
|
|
||||||
}
|
|
||||||
|
|
||||||
parent::__construct($level, $bubble);
|
parent::__construct($level, $bubble);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a connection resource is available
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function isConnected($scheme)
|
||||||
|
{
|
||||||
|
return $this->{$scheme . 'Connection'} !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Establish a connection to an UDP socket
|
* Establish a connection to an UDP socket
|
||||||
*
|
*
|
||||||
* @throws LogicException when unable to connect to the socket
|
* @throws LogicException when unable to connect to the socket
|
||||||
*/
|
*/
|
||||||
protected function connectUdp($host = '127.0.0.1', $port = 1180)
|
protected function connectUdp()
|
||||||
{
|
{
|
||||||
if (!extension_loaded('sockets')) {
|
if (!extension_loaded('sockets')) {
|
||||||
throw new \LogicException('The sockets extension is not loaded');
|
throw new \LogicException('The sockets extension is not loaded');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->socket = socket_create(AF_INET, SOCK_DGRAM, 0);
|
$this->udpConnection = socket_create(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (!$this->socket)
|
if (!$this->udpConnection) {
|
||||||
throw new \LogicException('Unable to create a socket');
|
throw new \LogicException('Unable to create a socket');
|
||||||
|
}
|
||||||
$result = socket_connect($this->socket, $host, $port);
|
|
||||||
if (!$result)
|
if (!socket_connect($this->udpConnection, $this->host, $this->port)) {
|
||||||
{
|
throw new \LogicException('Unable to connect to the socket at '
|
||||||
throw new \LogicException('Unable to connect to the socket at ' . $host . ':' . $port);
|
. $this->host . ':' . $this->port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Establish a connection to a http server
|
* Establish a connection to a http server
|
||||||
*/
|
*/
|
||||||
protected function connectHttp($host = 'localhost', $port = 1080)
|
protected function connectHttp()
|
||||||
{
|
{
|
||||||
$this->curlConnection = curl_init('http://'.$host.':'.$port.'/1.0/event/put');
|
$this->httpConnection =
|
||||||
|
curl_init('http://'.$this->host.':'.$this->port.'/1.0/event/put');
|
||||||
if(!$this->curlConnection)
|
|
||||||
throw new \LogicException('Unable to connect to ' . $host . ':' . $port);
|
if (!$this->httpConnection) {
|
||||||
|
throw new \LogicException('Unable to connect to '
|
||||||
curl_setopt($this->curlConnection, CURLOPT_CUSTOMREQUEST, "POST");
|
. $this->host . ':' . $this->port);
|
||||||
curl_setopt($this->curlConnection, CURLOPT_RETURNTRANSFER, true);
|
}
|
||||||
|
|
||||||
|
curl_setopt($this->httpConnection, CURLOPT_CUSTOMREQUEST, "POST");
|
||||||
|
curl_setopt($this->httpConnection, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send Event to Cube
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* This assumes that the mandatory fields :
|
|
||||||
* - type is in $record['context']['type']
|
|
||||||
* - data is $record['context']
|
|
||||||
*/
|
*/
|
||||||
protected function write(array $record)
|
protected function write(array $record)
|
||||||
{
|
{
|
||||||
$date = $record['datetime'];
|
$date = $record['datetime'];
|
||||||
|
|
||||||
$datas = array(
|
$datas = array('time' => $date->format('Y-m-d H:i:s'));
|
||||||
'type' => $record['context']['type'],
|
unset($record['datetime']);
|
||||||
'time' => $date->format('Y-m-d H:i:s')
|
|
||||||
);
|
if (isset($record['context']['type'])) {
|
||||||
|
$datas['type'] = $record['context']['type'];
|
||||||
unset($record['context']['type'], $record['datetime']);
|
unset($record['context']['type']);
|
||||||
|
}
|
||||||
$datas['data'] = $record['context'];
|
|
||||||
$datas['data']['level'] = $record['level'];
|
$datas['data'] = $record['context'];
|
||||||
|
$datas['data']['level'] = $record['level'];
|
||||||
$datas = json_encode($datas);
|
|
||||||
|
call_user_func(
|
||||||
switch($this->urlScheme)
|
array($this, 'send'.ucwords($this->scheme)), json_encode($datas));
|
||||||
{
|
}
|
||||||
case 'http' :
|
|
||||||
curl_setopt($this->curlConnection, CURLOPT_POSTFIELDS, '['.$datas.']');
|
|
||||||
curl_setopt($this->curlConnection, CURLOPT_HTTPHEADER, array(
|
private function sendUdp($datas)
|
||||||
'Content-Type: application/json',
|
{
|
||||||
'Content-Length: ' . strlen('['.$datas.']'))
|
if (!$this->isConnected($this->scheme)) {
|
||||||
);
|
call_user_func(
|
||||||
$result = curl_exec($this->curlConnection);
|
array($this, 'connect' . ucwords($this->scheme)));
|
||||||
break;
|
}
|
||||||
case 'udp' : socket_send($this->socket, $datas, strlen($datas), 0);
|
|
||||||
}
|
socket_send($this->udpConnection, $datas, strlen($datas), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function sendHttp($datas)
|
||||||
|
{
|
||||||
|
if (!$this->isConnected($this->scheme)) {
|
||||||
|
call_user_func(
|
||||||
|
array($this, 'connect' . ucwords($this->scheme)));
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$datas.']');
|
||||||
|
curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, array(
|
||||||
|
'Content-Type: application/json',
|
||||||
|
'Content-Length: ' . strlen('['.$datas.']'))
|
||||||
|
);
|
||||||
|
return curl_exec($this->httpConnection);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user