Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Slack | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getBody | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * HTTP POST log writer for Slack |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Error_Logging |
25 | * @author Chris Hallberg <challber@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Log\Writer; |
31 | |
32 | use Laminas\Http\Client; |
33 | |
34 | /** |
35 | * This class extends the Laminas Logging to send errors to Slack |
36 | * |
37 | * @category VuFind |
38 | * @package Error_Logging |
39 | * @author Chris Hallberg <challber@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org Main Site |
42 | */ |
43 | class Slack extends Post |
44 | { |
45 | /** |
46 | * The slack channel that should receive messages. |
47 | * |
48 | * @var string |
49 | */ |
50 | protected $channel = '#vufind_log'; |
51 | |
52 | /** |
53 | * The slack username messages are posted under. |
54 | * |
55 | * @var string |
56 | */ |
57 | protected $username = 'VuFind Log'; |
58 | |
59 | /** |
60 | * Icons that appear at the start of log messages in Slack, by severity |
61 | * |
62 | * @var array |
63 | */ |
64 | protected $messageIcons = [ |
65 | ':fire: :fire: :fire: ', // EMERG |
66 | ':rotating_light: ', // ALERT |
67 | ':red_circle: ', // CRIT |
68 | ':exclamation: ', // ERR |
69 | ':warning: ', // WARN |
70 | ':speech_balloon: ', // NOTICE |
71 | ':information_source: ', // INFO |
72 | ':beetle: ', // DEBUG |
73 | ]; |
74 | |
75 | /** |
76 | * Constructor |
77 | * |
78 | * @param string $url URL to open as a stream |
79 | * @param Client $client Pre-configured http client |
80 | * @param array $options Optional settings (may contain 'channel' for the |
81 | * Slack channel to use and/or 'name' for the username messages are posted under) |
82 | */ |
83 | public function __construct($url, Client $client, array $options = []) |
84 | { |
85 | if (isset($options['channel'])) { |
86 | $this->channel = $options['channel']; |
87 | } |
88 | if (isset($options['name'])) { |
89 | $this->username = $options['name']; |
90 | } |
91 | parent::__construct($url, $client); |
92 | } |
93 | |
94 | /** |
95 | * Get data for raw body |
96 | * |
97 | * @param array $event event data |
98 | * |
99 | * @return string |
100 | */ |
101 | protected function getBody($event) |
102 | { |
103 | $data = [ |
104 | 'channel' => $this->channel, |
105 | 'username' => $this->username, |
106 | 'text' => $this->messageIcons[$event['priority']] |
107 | . $this->formatter->format($event) . PHP_EOL, |
108 | ]; |
109 | return json_encode($data); |
110 | } |
111 | } |