/
XMPPLogger.php
197 lines (177 loc) · 4.59 KB
/
XMPPLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
*
* This file is licensed under the MIT License. See the LICENSE file.
*
* @author Dmitry Volynkin <thesaturn@thesaturn.me>
*/
namespace thesaturn\xmpplogger;
use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Protocol\Message;
use \Psr\Log\LogLevel;
class XMPPLogger implements \Psr\Log\LoggerInterface
{
/**
* XMPP client to send messages
* @var Client
*/
private $xmppClient;
/**
* @var callable
* @see Echolog::defaultMessageFormatter()
*/
private $messageFormatter;
/**
* @var string
*/
private $level;
/**
* @var string
*/
private $toJID;
/**
* @var int[]
*/
protected static $rankings = array(
LogLevel::DEBUG => 7,
LogLevel::INFO => 6,
LogLevel::NOTICE => 5,
LogLevel::WARNING => 4,
LogLevel::ERROR => 3,
LogLevel::CRITICAL => 2,
LogLevel::ALERT => 1,
LogLevel::EMERGENCY => 0,
);
/**
* @param Client $client
* @param string $toJID
* @param string $level
*/
public function __construct(Client $client, $toJID, $level = LogLevel::DEBUG)
{
$this->xmppClient = $client;
$this->toJID = $toJID;
$this->level = $level;
$this->messageFormatter = array($this, 'defaultMessageFormatter');
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function emergency($message, array $context = array())
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function alert($message, array $context = array())
{
$this->log(LogLevel::ALERT, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function critical($message, array $context = array())
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function error($message, array $context = array())
{
$this->log(LogLevel::ERROR, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function warning($message, array $context = array())
{
$this->log(LogLevel::WARNING, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function notice($message, array $context = array())
{
$this->log(LogLevel::NOTICE, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function info($message, array $context = array())
{
$this->log(LogLevel::INFO, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return void
*/
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}
/**
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = array())
{
$hasLevel = isset(self::$rankings[$level]);
if (!$hasLevel || ($hasLevel && self::$rankings[$level] <= self::$rankings[$this->level])) {
$formatter = $this->messageFormatter;
$message = new Message($formatter($level, $message, $context), $this->toJID);
if(!$this->xmppClient->getConnection()->isConnected()) {
$this->xmppClient->connect();
}
$this->xmppClient->send($message);
}
}
/**
* @param string $level
* @param string $message
* @param array $context
* @return string
*/
protected function defaultMessageFormatter($level, $message, array $context = array())
{
$message = sprintf('[%s] %s %s', date('Y-m-d H:i:s'), strtoupper($level), $message);
if ($context) {
$message .= ' ' . json_encode($context);
}
return $message;
}
/**
* @param callable $messageFormatter
* @return void
*/
public function setMessageFormatter(callable $messageFormatter)
{
$this->messageFormatter = $messageFormatter;
}
/**
* @param string $level
* @return void
*/
public function setLevel($level)
{
$this->level = $level;
}
}