]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/src/Monolog/Handler/IFTTTHandler.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / src / Monolog / Handler / IFTTTHandler.php
1 <?php
2
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Monolog\Handler;
13
14 use Monolog\Logger;
15
16 /**
17  * IFTTTHandler uses cURL to trigger IFTTT Maker actions
18  *
19  * Register a secret key and trigger/event name at https://ifttt.com/maker
20  *
21  * value1 will be the channel from monolog's Logger constructor,
22  * value2 will be the level name (ERROR, WARNING, ..)
23  * value3 will be the log record's message
24  *
25  * @author Nehal Patel <nehal@nehalpatel.me>
26  */
27 class IFTTTHandler extends AbstractProcessingHandler
28 {
29     private $eventName;
30     private $secretKey;
31
32     /**
33      * @param string  $eventName The name of the IFTTT Maker event that should be triggered
34      * @param string  $secretKey A valid IFTTT secret key
35      * @param int     $level     The minimum logging level at which this handler will be triggered
36      * @param Boolean $bubble    Whether the messages that are handled can bubble up the stack or not
37      */
38     public function __construct($eventName, $secretKey, $level = Logger::ERROR, $bubble = true)
39     {
40         $this->eventName = $eventName;
41         $this->secretKey = $secretKey;
42
43         parent::__construct($level, $bubble);
44     }
45
46     /**
47      * {@inheritdoc}
48      */
49     public function write(array $record)
50     {
51         $postData = array(
52             "value1" => $record["channel"],
53             "value2" => $record["level_name"],
54             "value3" => $record["message"],
55         );
56         $postString = json_encode($postData);
57
58         $ch = curl_init();
59         curl_setopt($ch, CURLOPT_URL, "https://maker.ifttt.com/trigger/" . $this->eventName . "/with/key/" . $this->secretKey);
60         curl_setopt($ch, CURLOPT_POST, true);
61         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62         curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
63         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
64             "Content-Type: application/json",
65         ));
66
67         Curl\Util::execute($ch);
68     }
69 }