This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0e04ba
commit d83ba1a
Showing
6 changed files
with
326 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea | ||
/vendor | ||
composer.phar | ||
phpunit.phar | ||
/phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Sergey Makinen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,69 @@ | ||
# yii2-slack-log | ||
Pretty Slack log target for Yii 2 | ||
# Slack log target for Yii 2 | ||
|
||
Pretty [Slack](https://slack.com) log target for Yii 2. | ||
|
||
![Screenshot](README.png) | ||
|
||
[![Code Quality](https://img.shields.io/scrutinizer/g/sergeymakinen/yii2-slack-log.svg?style=flat-square)](https://scrutinizer-ci.com/g/sergeymakinen/yii2-slack-log) [![Packagist Version](https://img.shields.io/packagist/v/sergeymakinen/yii2-slack-log.svg?style=flat-square)](https://packagist.org/packages/sergeymakinen/yii2-slack-log) [![Total Downloads](https://img.shields.io/packagist/dt/sergeymakinen/yii2-slack-log.svg?style=flat-square)](https://packagist.org/packages/sergeymakinen/yii2-slack-log) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) | ||
|
||
## Installation | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require sergeymakinen/yii2-slack-log "^1.0" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"sergeymakinen/yii2-slack-log": "^1.0" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
## Usage | ||
|
||
First set up an [incoming webhook integration](https://my.slack.com/services/new/incoming-webhook/) in your Slack team and obtain a token. It should look like `https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX`. | ||
|
||
Then set the following Yii 2 configuration parameters: | ||
|
||
```php | ||
'components' => [ | ||
'log' => [ | ||
'targets' => [ | ||
[ | ||
'class' => 'sergeymakinen\log\SlackTarget', | ||
'exportInterval' => 50, // 50 or less is better | ||
'webhookUrl' => 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX', | ||
], | ||
], | ||
], | ||
], | ||
``` | ||
|
||
Sample config: | ||
|
||
```php | ||
'components' => [ | ||
'log' => [ | ||
'targets' => [ | ||
[ | ||
'class' => 'sergeymakinen\log\SlackTarget', | ||
'levels' => ['error'], | ||
'except' => [ | ||
'yii\web\HttpException', | ||
'yii\web\HttpException:404', | ||
], | ||
'exportInterval' => 50, | ||
'enabled' => YII_ENV_PROD, | ||
'webhookUrl' => 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX', | ||
'username' => 'Fire Alarm Bot', | ||
'iconEmoji' => ':poop:', | ||
], | ||
], | ||
], | ||
], | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
|
||
namespace sergeymakinen\log; | ||
|
||
use yii\helpers\Json; | ||
use yii\helpers\Url; | ||
use yii\helpers\VarDumper; | ||
use yii\log\Logger; | ||
use yii\log\Target; | ||
use yii\web\Request; | ||
|
||
class SlackTarget extends Target | ||
{ | ||
/** | ||
* Incoming Webhook URL. | ||
* | ||
* @var string | ||
*/ | ||
public $webhookUrl; | ||
|
||
/** | ||
* Displayed username. | ||
* | ||
* @var string | ||
*/ | ||
public $username; | ||
|
||
/** | ||
* Icon URL. | ||
* | ||
* @var string | ||
*/ | ||
public $iconUrl; | ||
|
||
/** | ||
* Icon Emoji. | ||
* | ||
* @var string | ||
*/ | ||
public $iconEmoji; | ||
|
||
/** | ||
* Channel or Direct Message. | ||
* | ||
* @var string | ||
*/ | ||
public $channel; | ||
|
||
/** | ||
* Colors per a Logger level. | ||
* | ||
* @var array | ||
*/ | ||
public $colors = [ | ||
Logger::LEVEL_ERROR => 'danger', | ||
Logger::LEVEL_WARNING => 'warning' | ||
]; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function export() | ||
{ | ||
if (!isset($this->webhookUrl)) { | ||
return; | ||
} | ||
|
||
$payload = [ | ||
'parse' => 'none', | ||
'attachments' => array_map([$this, 'formatMessageAttachment'], $this->messages) | ||
]; | ||
if (isset($this->username)) { | ||
$payload['username'] = $this->username; | ||
} | ||
if (isset($this->iconUrl)) { | ||
$payload['icon_url'] = $this->iconUrl; | ||
} | ||
if (isset($this->iconEmoji)) { | ||
$payload['icon_emoji'] = $this->iconEmoji; | ||
} | ||
if (isset($this->channel)) { | ||
$payload['channel'] = $this->channel; | ||
} | ||
$context = stream_context_create([ | ||
'http' => [ | ||
'method' => 'POST', | ||
'header' => "Content-Type: application/json; charset=UTF-8\r\n", | ||
'content' => Json::encode($payload) | ||
] | ||
]); | ||
@file_get_contents($this->webhookUrl, false, $context); | ||
} | ||
|
||
/** | ||
* Encodes special chars in a message as HTML entities. | ||
* | ||
* @param string $message | ||
* | ||
* @return string | ||
*/ | ||
protected static function encodeMessage($message) | ||
{ | ||
return htmlspecialchars($message, ENT_NOQUOTES, 'UTF-8'); | ||
} | ||
|
||
/** | ||
* Returns a properly formatted message attachment for Slack API. | ||
* | ||
* @param array $message | ||
* | ||
* @return array | ||
*/ | ||
protected function formatMessageAttachment($message) | ||
{ | ||
list($text, $level, $category, $timestamp) = $message; | ||
$attachment = [ | ||
'fallback' => static::encodeMessage($this->formatMessage($message)), | ||
'title' => ucwords(Logger::getLevelName($level)), | ||
'fields' => [ | ||
[ | ||
'title' => 'Level', | ||
'value' => Logger::getLevelName($level), | ||
'short' => true | ||
], | ||
[ | ||
'title' => 'Category', | ||
'value' => '`' . static::encodeMessage($category) . '`', | ||
'short' => true | ||
] | ||
], | ||
'footer' => static::class, | ||
'ts' => (integer) round($timestamp), | ||
'mrkdwn_in' => [ | ||
'fields', | ||
'text' | ||
] | ||
]; | ||
if ($this->prefix !== null) { | ||
$attachment['fields'][] = [ | ||
'title' => 'Prefix', | ||
'value' => '`' . static::encodeMessage(call_user_func($this->prefix, $message)) . '`', | ||
'short' => true, | ||
]; | ||
} | ||
if (isset(\Yii::$app)) { | ||
if (isset($_SERVER['argv'])) { | ||
$attachment['author_name'] = implode(' ', $_SERVER['argv']); | ||
} elseif (\Yii::$app->request instanceof Request) { | ||
$attachment['author_name'] = Url::current([], true); | ||
$attachment['author_link'] = $attachment['author_name']; | ||
$attachment['fields'][] = [ | ||
'title' => 'User IP', | ||
'value' => \Yii::$app->request->userIP, | ||
'short' => true, | ||
]; | ||
} | ||
if (\Yii::$app->has('user', true) && isset(\Yii::$app->user)) { | ||
$user = \Yii::$app->user->getIdentity(false); | ||
if (isset($user)) { | ||
$attachment['fields'][] = [ | ||
'title' => 'User ID', | ||
'value' => $user->getId(), | ||
'short' => true, | ||
]; | ||
} | ||
} | ||
if (\Yii::$app->has('session', true) && isset(\Yii::$app->session)) { | ||
if (\Yii::$app->session->isActive) { | ||
$attachment['fields'][] = [ | ||
'title' => 'Session ID', | ||
'value' => \Yii::$app->session->id, | ||
'short' => true, | ||
]; | ||
} | ||
} | ||
} | ||
if (isset($this->colors[$level])) { | ||
$attachment['color'] = $this->colors[$level]; | ||
} | ||
if (!is_string($text)) { | ||
if ($text instanceof \Throwable || $text instanceof \Exception) { | ||
$text = (string) $text; | ||
} else { | ||
$text = VarDumper::export($text); | ||
} | ||
} | ||
$attachment['text'] = "```\n" . static::encodeMessage($text) . "\n```"; | ||
$traces = []; | ||
if (isset($message[4])) { | ||
foreach ($message[4] as $trace) { | ||
$traces[] = "in {$trace['file']}:{$trace['line']}"; | ||
} | ||
} | ||
if (!empty($traces)) { | ||
$attachment['fields'][] = [ | ||
'title' => 'Stack Trace', | ||
'value' => "```\n" . static::encodeMessage(implode("\n", $traces)) . "\n```", | ||
'short' => false, | ||
]; | ||
} | ||
return $attachment; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "sergeymakinen/yii2-slack-log", | ||
"homepage": "https://github.com/sergeymakinen/yii2-slack-log", | ||
"description": "Pretty Slack log target for Yii 2", | ||
"keywords": ["yii2-slack-log", "yii2", "yii", "slack", "log", "logging"], | ||
"type": "yii2-extension", | ||
"license": "MIT", | ||
"support": { | ||
"issues": "https://github.com/sergeymakinen/yii2-slack-log/issues?state=open", | ||
"source": "https://github.com/sergeymakinen/yii2-slack-log" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Sergey Makinen", | ||
"email": "[email protected]", | ||
"homepage": "https://makinen.ru" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"yiisoft/yii2": "2.0.*" | ||
}, | ||
"autoload": { | ||
"classmap": [ | ||
"SlackTarget.php" | ||
] | ||
} | ||
} |