Skip to content

Commit

Permalink
Split messaging layer from logger layer into 2nd package biteit/simpl…
Browse files Browse the repository at this point in the history
…e-slack-messenger
  • Loading branch information
manweCZ committed Mar 14, 2019
1 parent 385b5e6 commit b95a2bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 59 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "biteit/tracy-slack-logger",
"description": "Integrates Tracy Logger to your Slack",
"minimum-stability": "dev",
"version": "0.3.0",
"version": "0.4.0",
"license": "MIT",
"authors": [
{
Expand All @@ -12,7 +12,8 @@
],
"require": {
"tracy/tracy": "2.4.10",
"php": ">=5.6.0"
"php": ">=5.6.0",
"biteit/simple-slack-messenger": "dev-master"
},
"autoload": {
"psr-0": {
Expand Down
74 changes: 17 additions & 57 deletions src/BiteIT/TracySlackLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ class TracySlackLogger extends Logger {
protected $slackUsername;
protected $useFileLoggerAsWell = true;

protected $messenger = null;
protected $message = null;

public function __construct($webhookURL, $useDefaultLogger = false)
{
parent::__construct(Debugger::$logDirectory, Debugger::$email, Debugger::getBlueScreen());
$this->slackWebhookURL = $webhookURL;
$this->messenger = new SimpleSlackMessenger($webhookURL);
$this->useFileLoggerAsWell = $useDefaultLogger;
$this->message = new SimpleSlackMessage();
}

public function addReportingLevel($level){
Expand All @@ -40,8 +44,8 @@ public function setReportingLevels($levels){
* @return $this
*/
public function setSlackIconURL($slackIconURL){
$this->slackIconEmoji = null;
$this->slackIconURL = $slackIconURL;
$this->message->setIconEmoji(null);
$this->message->setIconUrl($slackIconURL);
return $this;
}

Expand All @@ -50,8 +54,8 @@ public function setSlackIconURL($slackIconURL){
* @return $this
*/
public function setSlackIconEmoji($emoji){
$this->slackIconEmoji = $emoji;
$this->slackIconURL = null;
$this->message->setIconEmoji($emoji);
$this->message->setIconUrl(null);
return $this;
}

Expand All @@ -60,7 +64,7 @@ public function setSlackIconEmoji($emoji){
* @return $this
*/
public function setSlackUsername($slackUsername){
$this->slackUsername = $slackUsername;
$this->message->setName($slackUsername);
return $this;
}

Expand All @@ -86,60 +90,16 @@ function log($value, $priority = self::INFO)

$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

$payload['text'] = "*{$priority}* on *{$_SERVER['HTTP_HOST']}* (URL: $url): $value";
if($this->slackUsername)
$payload['username'] = $this->slackUsername;
if($this->slackIconURL)
$payload['icon_url'] = $this->slackIconURL;
if($this->slackIconEmoji)
$payload['icon_emoji'] = $this->slackIconEmoji;

$message = array(
'payload' => json_encode($payload)
);
// Use curl to send your message
try {
if (ini_get('allow_url_fopen')) {
$result = $this->sendByFileGetContents($message);
} else {
$result = $this->sendByCurl($message);
}
$text = "*{$priority}* on *{$_SERVER['HTTP_HOST']}* (URL: $url): $value";
$this->message->setText($text);

try{
$this->messenger->sendSimpleMessage($this->message);
}
catch (\Exception $e){
echo 'Unable to use either file_get_conents nor curl';
catch (SimpleSlackException $exception){
throw $exception;
}


}

protected function sendByCurl($message){
$c = curl_init($this->slackWebhookURL);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $message);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
curl_close($c);

return $result;
}

protected function sendByFileGetContents($message){
$postdata = http_build_query(
$message
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$result = file_get_contents($this->slackWebhookURL, false, $context);
return $result;
}
}

0 comments on commit b95a2bc

Please sign in to comment.