ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SlackWebhookHandler.php
Go to the documentation of this file.
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 
15 use Monolog\Logger;
17 
25 {
30  private $webhookUrl;
31 
36  private $slackRecord;
37 
50  public function __construct($webhookUrl, $channel = null, $username = null, $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, $level = Logger::CRITICAL, $bubble = true, array $excludeFields = array())
51  {
52  parent::__construct($level, $bubble);
53 
54  $this->webhookUrl = $webhookUrl;
55 
56  $this->slackRecord = new SlackRecord(
57  $channel,
58  $username,
59  $useAttachment,
60  $iconEmoji,
61  $useShortAttachment,
62  $includeContextAndExtra,
63  $excludeFields,
64  $this->formatter
65  );
66  }
67 
68  public function getSlackRecord()
69  {
70  return $this->slackRecord;
71  }
72 
73  public function getWebhookUrl()
74  {
75  return $this->webhookUrl;
76  }
77 
83  protected function write(array $record)
84  {
85  $postData = $this->slackRecord->getSlackData($record);
86  $postString = json_encode($postData);
87 
88  $ch = curl_init();
89  $options = array(
90  CURLOPT_URL => $this->webhookUrl,
91  CURLOPT_POST => true,
92  CURLOPT_RETURNTRANSFER => true,
93  CURLOPT_HTTPHEADER => array('Content-type: application/json'),
94  CURLOPT_POSTFIELDS => $postString
95  );
96  if (defined('CURLOPT_SAFE_UPLOAD')) {
97  $options[CURLOPT_SAFE_UPLOAD] = true;
98  }
99 
100  curl_setopt_array($ch, $options);
101 
102  Curl\Util::execute($ch);
103  }
104 
106  {
107  parent::setFormatter($formatter);
108  $this->slackRecord->setFormatter($formatter);
109 
110  return $this;
111  }
112 
113  public function getFormatter()
114  {
115  $formatter = parent::getFormatter();
116  $this->slackRecord->setFormatter($formatter);
117 
118  return $formatter;
119  }
120 }
Base Handler class providing the Handler structure.
Sends notifications through Slack Webhooks.
static execute($ch, $retries=5, $closeAfterDone=true)
Executes a CURL request with optional retries and exception on failure.
Definition: Util.php:32
setFormatter(FormatterInterface $formatter)
Sets the formatter.
Slack record utility helping to log to Slack webhooks or API.
Definition: SlackRecord.php:26
if($session===NULL) $postData
const CRITICAL
Critical conditions.
Definition: Logger.php:65
__construct($webhookUrl, $channel=null, $username=null, $useAttachment=true, $iconEmoji=null, $useShortAttachment=false, $includeContextAndExtra=false, $level=Logger::CRITICAL, $bubble=true, array $excludeFields=array())