|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pageon\SlackWebhookMonolog\Slack\Attachment; |
|
4
|
|
|
|
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
use Monolog\Logger; |
|
7
|
|
|
use Pageon\SlackWebhookMonolog\General\Url; |
|
8
|
|
|
use Pageon\SlackWebhookMonolog\Monolog\Interfaces\ErrorInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* An attachment containing basic info about the error. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jelmer Prins <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @since 0.4.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class BasicInfoAttachment extends Attachment |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array The data from the error handler |
|
21
|
|
|
*/ |
|
22
|
|
|
private $record; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Extra error data. |
|
26
|
|
|
* |
|
27
|
|
|
* @var ErrorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $error; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* BasicInfoAttachment constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $record |
|
35
|
|
|
* @param ErrorInterface $error |
|
36
|
|
|
*/ |
|
37
|
8 |
|
public function __construct(array $record, ErrorInterface $error = null) |
|
38
|
|
|
{ |
|
39
|
8 |
|
$this->record = $record; |
|
40
|
8 |
|
$this->error = $error; |
|
41
|
|
|
|
|
42
|
8 |
|
$message = ($this->error !== null) ? $this->error->getMessage() : $this->record['message']; |
|
43
|
8 |
|
parent::__construct($message); |
|
44
|
|
|
|
|
45
|
8 |
|
$this->setColour($this->getColourForLoggerLevel()); |
|
46
|
8 |
|
$this->setText(sprintf('*Error:* %s', $this->record['level_name'])); |
|
47
|
8 |
|
$this->addField(new Field('What', $message)); |
|
48
|
|
|
|
|
49
|
8 |
|
$this->addField(new Field('When', $this->record['datetime']->format('d/m/Y H:m:i'), true)); |
|
50
|
|
|
|
|
51
|
8 |
|
if ($this->error !== null) { |
|
52
|
6 |
|
$this->addField(new Field('Line', $this->error->getLine(), true)); |
|
53
|
6 |
|
$this->addField(new Field('File', $this->error->getFile())); |
|
54
|
6 |
|
} |
|
55
|
8 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returned a Slack message attachment color associated with |
|
59
|
|
|
* provided level. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Colour |
|
62
|
|
|
*/ |
|
63
|
8 |
|
private function getColourForLoggerLevel() |
|
64
|
|
|
{ |
|
65
|
8 |
|
switch (true) { |
|
66
|
8 |
|
case $this->record['level'] >= Logger::ERROR: |
|
67
|
8 |
|
return new Colour('danger'); |
|
68
|
1 |
|
case $this->record['level'] >= Logger::WARNING: |
|
69
|
1 |
|
return new Colour('warning'); |
|
70
|
1 |
|
case $this->record['level'] >= Logger::INFO: |
|
71
|
1 |
|
return new Colour('good'); |
|
72
|
1 |
|
default: |
|
73
|
1 |
|
return new Colour('#e3e4e6'); |
|
74
|
1 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|