1 | <?php |
||
22 | class SlackTarget extends Target |
||
23 | { |
||
24 | /** |
||
25 | * Yii HTTP client configuration. |
||
26 | * This can be a component ID, a configuration array or a Client instance. |
||
27 | * |
||
28 | * @var Client|array|string |
||
29 | * @since 1.2 |
||
30 | */ |
||
31 | public $httpClient = [ |
||
32 | 'class' => 'yii\httpclient\Client', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Incoming Webhook URL. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $webhookUrl; |
||
41 | |||
42 | /** |
||
43 | * Displayed username. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $username; |
||
48 | |||
49 | /** |
||
50 | * Icon URL. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | public $iconUrl; |
||
55 | |||
56 | /** |
||
57 | * Icon Emoji. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | public $iconEmoji; |
||
62 | |||
63 | /** |
||
64 | * Channel or Direct Message. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | public $channel; |
||
69 | |||
70 | /** |
||
71 | * Colors per a Logger level. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | public $colors = [ |
||
76 | Logger::LEVEL_ERROR => 'danger', |
||
77 | Logger::LEVEL_WARNING => 'warning', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * @inheritDoc |
||
82 | */ |
||
83 | public $exportInterval = 50; |
||
84 | |||
85 | /** |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | 5 | public function init() |
|
93 | |||
94 | /** |
||
95 | * @inheritDoc |
||
96 | */ |
||
97 | 1 | public function export() |
|
110 | |||
111 | /** |
||
112 | * Encodes special chars in a message as HTML entities. |
||
113 | * |
||
114 | * @param string $message |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 4 | protected function encodeMessage($message) |
|
122 | |||
123 | /** |
||
124 | * Returns a Slack API payload. |
||
125 | * |
||
126 | * @return array |
||
127 | * @since 1.2 |
||
128 | */ |
||
129 | 2 | protected function getPayload() |
|
141 | |||
142 | /** |
||
143 | * Returns a properly formatted message attachment for Slack API. |
||
144 | * |
||
145 | * @param array $message |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 3 | protected function formatMessageAttachment($message) |
|
150 | { |
||
151 | 3 | list($text, $level, $category, $timestamp) = $message; |
|
152 | $attachment = [ |
||
153 | 3 | 'fallback' => $this->encodeMessage($this->formatMessage($message)), |
|
154 | 3 | 'title' => ucwords(Logger::getLevelName($level)), |
|
155 | 'fields' => [ |
||
156 | [ |
||
157 | 3 | 'title' => 'Level', |
|
158 | 3 | 'value' => Logger::getLevelName($level), |
|
159 | 3 | 'short' => true, |
|
160 | 3 | ], |
|
161 | [ |
||
162 | 3 | 'title' => 'Category', |
|
163 | 3 | 'value' => '`' . $this->encodeMessage($category) . '`', |
|
164 | 3 | 'short' => true, |
|
165 | 3 | ], |
|
166 | 3 | ], |
|
167 | 3 | 'footer' => static::className(), |
|
168 | 3 | 'ts' => (int) round($timestamp), |
|
169 | 'mrkdwn_in' => [ |
||
170 | 3 | 'fields', |
|
171 | 3 | 'text', |
|
172 | 3 | ], |
|
173 | 3 | ]; |
|
174 | 3 | if (isset($this->prefix)) { |
|
175 | 3 | $attachment['fields'][] = [ |
|
176 | 3 | 'title' => 'Prefix', |
|
177 | 3 | 'value' => '`' . $this->encodeMessage(call_user_func($this->prefix, $message)) . '`', |
|
178 | 3 | 'short' => true, |
|
179 | ]; |
||
180 | 3 | } |
|
181 | 3 | if (isset(\Yii::$app)) { |
|
182 | 3 | $this->insertRequestIntoAttachment($attachment); |
|
183 | 3 | $this->insertUserIntoAttachment($attachment); |
|
184 | 3 | $this->insertSessionIntoAttachment($attachment); |
|
185 | 3 | } |
|
186 | 3 | if (isset($this->colors[$level])) { |
|
187 | 2 | $attachment['color'] = $this->colors[$level]; |
|
188 | 2 | } |
|
189 | 3 | if (!is_string($text)) { |
|
190 | 2 | if ($text instanceof \Throwable || $text instanceof \Exception) { |
|
191 | 1 | $text = (string) $text; |
|
192 | 1 | } else { |
|
193 | 1 | $text = VarDumper::export($text); |
|
194 | } |
||
195 | 2 | } |
|
196 | 3 | $attachment['text'] = "```\n" . $this->encodeMessage($text) . "\n```"; |
|
197 | 3 | if (isset($message[4]) && !empty($message[4])) { |
|
198 | 1 | $this->insertTracesIntoAttachment($message[4], $attachment); |
|
199 | 1 | } |
|
200 | 3 | return $attachment; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Inserts session data into the attachement if applicable. |
||
205 | * |
||
206 | * @param array $attachment |
||
207 | */ |
||
208 | 3 | private function insertSessionIntoAttachment(array &$attachment) |
|
222 | |||
223 | /** |
||
224 | * Inserts traces into the attachement if applicable. |
||
225 | * |
||
226 | * @param array $traces |
||
227 | * @param array $attachment |
||
228 | */ |
||
229 | private function insertTracesIntoAttachment(array $traces, array &$attachment) |
||
240 | |||
241 | /** |
||
242 | * Inserts user data into the attachement if applicable. |
||
243 | * |
||
244 | * @param array $attachment |
||
245 | */ |
||
246 | 3 | private function insertUserIntoAttachment(array &$attachment) |
|
259 | |||
260 | /** |
||
261 | * Inserts request data into the attachement if applicable. |
||
262 | * |
||
263 | * @param array $attachment |
||
264 | */ |
||
265 | 4 | private function insertRequestIntoAttachment(array &$attachment) |
|
283 | |||
284 | /** |
||
285 | * Copies the value to the payload if the value is set. |
||
286 | * |
||
287 | * @param array $payload |
||
288 | * @param string $name |
||
289 | * @param string $value |
||
290 | */ |
||
291 | 2 | private function insertIntoPayload(array &$payload, $name, $value) |
|
297 | } |
||
298 |