1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\Notify\Drivers; |
4
|
|
|
|
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Monolog\Handler\HandlerInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Tylercd100\Notify\Factories\MonologHandlerFactory as Factory; |
9
|
|
|
|
10
|
|
|
abstract class Base implements LoggerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Logger |
19
|
|
|
*/ |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $title; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $config An array of config values to overwrite |
29
|
|
|
* @param Logger $logger A Monolog instance to use |
30
|
|
|
* @param string $title The title to set for the handlers |
31
|
|
|
*/ |
32
|
36 |
|
public function __construct(array $config = [], Logger $logger = null, $title = ""){ |
33
|
|
|
//Merge the existing config with the provided config |
34
|
36 |
|
$default = is_array(config('notify')) ? config('notify') : []; |
35
|
36 |
|
$this->config = array_merge($default,$config); |
36
|
|
|
|
37
|
36 |
|
if(!$logger instanceof Logger){ |
38
|
33 |
|
$logger = new Logger($this->config['channel']); |
39
|
33 |
|
} |
40
|
36 |
|
$this->logger = $logger; |
41
|
36 |
|
$this->title = $title; |
42
|
|
|
|
43
|
36 |
|
$this->attachDrivers(); |
44
|
36 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a list of driver names to load |
48
|
|
|
* @return array An array of drivers to use |
49
|
|
|
*/ |
50
|
|
|
abstract protected function getDrivers(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets a handler instance for the provided name |
54
|
|
|
* @param string $name The name of the driver you want to use |
55
|
|
|
* @return HandlerInterface |
56
|
|
|
*/ |
57
|
36 |
|
protected function getHandlerInstanceByName($name){ |
58
|
36 |
|
$config = (isset($this->config[$name]) ? $this->config[$name] : []); |
59
|
36 |
|
return Factory::create($name,$config,$this->title); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Pushes a Monolog Handler in to the Monolog Logger instance |
64
|
|
|
* @param HandlerInterface $handler The Handler to attach |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
36 |
|
protected function attachHandler(HandlerInterface $handler){ |
68
|
36 |
|
$this->logger->pushHandler($handler); |
69
|
36 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* This will attach all the monolog handlers specified in the drivers config array |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
36 |
|
protected function attachDrivers() |
76
|
|
|
{ |
77
|
36 |
|
$drivers = $this->getDrivers(); |
78
|
36 |
|
foreach ($drivers as $driver) { |
79
|
36 |
|
$handler = $this->getHandlerInstanceByName($driver); |
80
|
36 |
|
$this->attachHandler($handler); |
81
|
36 |
|
} |
82
|
36 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* System is unusable. |
86
|
|
|
* |
87
|
|
|
* @param string $message |
88
|
|
|
* @param array $context |
89
|
|
|
* @return null |
90
|
|
|
*/ |
91
|
3 |
|
public function emergency($message, array $context = array()) |
92
|
|
|
{ |
93
|
3 |
|
$this->logger->emergency($message,$context); |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Action must be taken immediately. |
98
|
|
|
* |
99
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
100
|
|
|
* trigger the SMS alerts and wake you up. |
101
|
|
|
* |
102
|
|
|
* @param string $message |
103
|
|
|
* @param array $context |
104
|
|
|
* @return null |
105
|
|
|
*/ |
106
|
3 |
|
public function alert($message, array $context = array()) |
107
|
|
|
{ |
108
|
3 |
|
$this->logger->alert($message,$context); |
109
|
3 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Critical conditions. |
113
|
|
|
* |
114
|
|
|
* Example: Application component unavailable, unexpected exception. |
115
|
|
|
* |
116
|
|
|
* @param string $message |
117
|
|
|
* @param array $context |
118
|
|
|
* @return null |
119
|
|
|
*/ |
120
|
3 |
|
public function critical($message, array $context = array()) |
121
|
|
|
{ |
122
|
3 |
|
$this->logger->critical($message,$context); |
123
|
3 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Runtime errors that do not require immediate action but should typically |
127
|
|
|
* be logged and monitored. |
128
|
|
|
* |
129
|
|
|
* @param string $message |
130
|
|
|
* @param array $context |
131
|
|
|
* @return null |
132
|
|
|
*/ |
133
|
3 |
|
public function error($message, array $context = array()) |
134
|
|
|
{ |
135
|
3 |
|
$this->logger->error($message,$context); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Exceptional occurrences that are not errors. |
140
|
|
|
* |
141
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
142
|
|
|
* that are not necessarily wrong. |
143
|
|
|
* |
144
|
|
|
* @param string $message |
145
|
|
|
* @param array $context |
146
|
|
|
* @return null |
147
|
|
|
*/ |
148
|
3 |
|
public function warning($message, array $context = array()) |
149
|
|
|
{ |
150
|
3 |
|
$this->logger->warning($message,$context); |
151
|
3 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Normal but significant events. |
155
|
|
|
* |
156
|
|
|
* @param string $message |
157
|
|
|
* @param array $context |
158
|
|
|
* @return null |
159
|
|
|
*/ |
160
|
3 |
|
public function notice($message, array $context = array()) |
161
|
|
|
{ |
162
|
3 |
|
$this->logger->notice($message,$context); |
163
|
3 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Interesting events. |
167
|
|
|
* |
168
|
|
|
* Example: User logs in, SQL logs. |
169
|
|
|
* |
170
|
|
|
* @param string $message |
171
|
|
|
* @param array $context |
172
|
|
|
* @return null |
173
|
|
|
*/ |
174
|
3 |
|
public function info($message, array $context = array()) |
175
|
|
|
{ |
176
|
3 |
|
$this->logger->info($message,$context); |
177
|
3 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Detailed debug information. |
181
|
|
|
* |
182
|
|
|
* @param string $message |
183
|
|
|
* @param array $context |
184
|
|
|
* @return null |
185
|
|
|
*/ |
186
|
3 |
|
public function debug($message, array $context = array()) |
187
|
|
|
{ |
188
|
3 |
|
$this->logger->debug($message,$context); |
189
|
3 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Logs with an arbitrary level. |
193
|
|
|
* |
194
|
|
|
* @param mixed $level |
195
|
|
|
* @param string $message |
196
|
|
|
* @param array $context |
197
|
|
|
* @return null |
198
|
|
|
*/ |
199
|
|
|
public function log($level, $message, array $context = array()) |
200
|
|
|
{ |
201
|
|
|
$this->logger->log($level, $message, $context); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |