Skip to content

Commit 9f88f23

Browse files
committed
first commit
0 parents  commit 9f88f23

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/vendor
2+
/laravel
3+
/node_modules
4+
composer.phar
5+
composer.lock
6+
.DS_Store
7+
Thumbs.db
8+
phpunit.xml
9+
/.idea

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Georges KABBOUCHI
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Laravel Logger - Discord Channel
2+
###### A Discord based Monolog driver for Laravel
3+
4+
## Install
5+
```bash
6+
composer require kabbouchi/laravel-logger-discord-channel:dev-master
7+
8+
```
9+
10+
## Usage
11+
12+
Add the new driver type in your `config/logging.php` configuration
13+
14+
```php
15+
'channels' => [
16+
'discord' => [
17+
'driver' => 'custom',
18+
'via' => KABBOUCHI\LoggerDiscordChannel\DiscordLogger::class,
19+
'webhook' => 'https://discordapp.com/api/webhooks/.....',
20+
'level' => 'DEBUG'
21+
],
22+
],
23+
24+
```

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "kabbouchi/laravel-logger-discord-channel",
3+
"description": "A Discord based Monolog driver for Laravel",
4+
"keywords": [
5+
"laravel",
6+
"logger",
7+
"discord"
8+
],
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Georges KABBOUCHI",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"require": {
17+
"php": ">=7.1.0",
18+
"illuminate/contracts": "~5.6",
19+
"illuminate/queue": "~5.6",
20+
"illuminate/support": "~5.6",
21+
"guzzlehttp/guzzle": "^6.3",
22+
"monolog/monolog": "~1.12"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"KABBOUCHI\\LoggerDiscordChannel\\": "src/"
27+
}
28+
},
29+
"config": {
30+
"sort-packages": true
31+
},
32+
"prefer-stable": true,
33+
"minimum-stability": "dev"
34+
}

src/DiscordHandler.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace KABBOUCHI\LoggerDiscordChannel;
4+
5+
use Monolog\Formatter\LineFormatter;
6+
use \Monolog\Logger;
7+
use \Monolog\Handler\AbstractProcessingHandler;
8+
9+
class DiscordHandler extends AbstractProcessingHandler
10+
{
11+
private $initialized = false;
12+
private $guzzle;
13+
14+
private $name;
15+
private $subname;
16+
17+
private $webhook;
18+
private $statement;
19+
20+
/**
21+
* MonologDiscordHandler constructor.
22+
* @param \GuzzleHttp\Client $guzzle
23+
* @param bool $webhooks
24+
* @param int $level
25+
* @param bool $bubble
26+
*/
27+
public function __construct($webhook, $name, $subname = '', $level = Logger::DEBUG, $bubble = true)
28+
{
29+
$this->name = $name;
30+
$this->subname = $subname;
31+
$this->guzzle = new \GuzzleHttp\Client();
32+
$this->webhook = $webhook;
33+
parent::__construct($level, $bubble);
34+
}
35+
36+
/**
37+
* @param array $record
38+
* @throws \GuzzleHttp\Exception\GuzzleException
39+
*/
40+
protected function write(array $record)
41+
{
42+
$formatter = new LineFormatter(null, null, true, true);
43+
$formatter->includeStacktraces();
44+
45+
$content = $formatter->format($record);
46+
47+
48+
$this->guzzle->request('POST', $this->webhook, [
49+
'form_params' => [
50+
'content' => env('APP_URL') . " on fire 🔥 \n\n" . substr($content, 0, 1900)
51+
]
52+
]);
53+
}
54+
}

src/DiscordLogger.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace KABBOUCHI\LoggerDiscordChannel;
4+
5+
use Monolog\Logger;
6+
7+
class DiscordLogger
8+
{
9+
/**
10+
* Create a custom Monolog instance.
11+
*
12+
* @param array $config
13+
* @return \Monolog\Logger
14+
*/
15+
public function __invoke(array $config)
16+
{
17+
$log = new Logger('discord');
18+
$log->pushHandler(new DiscordHandler($config['webhook'], config('app.name'), null, $config['level'] ?? 'DEBUG'));
19+
20+
return $log;
21+
}
22+
}

0 commit comments

Comments
 (0)