|
1
|
|
|
<?php |
|
2
|
|
|
namespace Websoftwares\Monolog\Handler; |
|
3
|
|
|
/** |
|
4
|
|
|
* Sends your logs over a ZeroMQ (ØMQ) socket. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Websoftwares |
|
7
|
|
|
* @subpackage Monolog |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
* @author Boris <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
use Monolog\Logger; |
|
12
|
|
|
use Monolog\Formatter\JsonFormatter; |
|
13
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
|
14
|
|
|
|
|
15
|
|
|
class ZMQHandler extends AbstractProcessingHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \ZMQSocket |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $zmqSocket; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @see http://api.zeromq.org/4-0:zmq-sendmsg |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $zmqMode = \ZMQ::MODE_DONTWAIT; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var boolean |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $multipart = false; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param \zmqSocket $zmqSocket instance of \ZMQSocket for now only the send types allowed |
|
35
|
|
|
* @param int $zmqMode ZMQ mode |
|
36
|
|
|
* @param boolean $multipart send multipart message |
|
37
|
|
|
* @param int $level |
|
38
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
\zmqSocket $zmqSocket, |
|
42
|
|
|
$zmqMode = \ZMQ::MODE_DONTWAIT, |
|
43
|
|
|
$multipart = false, |
|
44
|
|
|
$level = Logger::DEBUG, |
|
45
|
|
|
$bubble = true) |
|
46
|
|
|
{ |
|
47
|
|
|
$zmqSocketType = $zmqSocket->getSocketType(); |
|
48
|
|
|
|
|
49
|
|
|
// If u need the the Bi-directional types make a PR |
|
50
|
|
|
if (! $zmqSocketType == \ZMQ::SOCKET_PUB || ! $zmqSocketType == \ZMQ::SOCKET_PUSH) { |
|
51
|
|
|
throw new \Exception("Invalid socket type used, only PUB, PUSH allowed."); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->zmqSocket = $zmqSocket; |
|
55
|
|
|
$this->zmqMode = $zmqMode; |
|
56
|
|
|
$this->multipart = $multipart; |
|
57
|
|
|
parent::__construct($level, $bubble); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function write(array $record) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->multipart) { |
|
66
|
|
|
$this->zmqSocket->send($record['channel'],$this->zmqMode); |
|
67
|
|
|
$this->zmqSocket->send($record['formatted']); |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->zmqSocket->send($record["formatted"],$this->zmqMode); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getDefaultFormatter() |
|
77
|
|
|
{ |
|
78
|
|
|
return new JsonFormatter(); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.