miraris/monolog-discord-handler

A simple Monolog handler for Discord webhooks

0.2.0 2018-04-01 12:48 UTC

This package is not auto-updated.

Last update: 2024-04-28 03:14:31 UTC


README

Packagist Packagist

A simple Monolog handler for Discord webhooks

Dependecies

  • PHP >= 5.3
  • Monolog >= 1.3

1. Installing

The recommended way to install monolog-discord-handler is through Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of monolog-discord-handler:

php composer.phar require miraris/monolog-discord-handler

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

You can then later update monolog-discord-handler using composer:

composer.phar update

2. Usage

Push this handler to your Monolog instance:

Single webhook URL

<?php
require 'vendor/autoload.php';

$log = new Monolog\Logger('name');

$log->pushHandler(new DiscordHandler\DiscordHandler([
    'https://discordapp.com/api/webhooks/xxx/yyy'
], 'DEBUG'));

Multiple webhook URLs

<?php
require 'vendor/autoload.php';

$log = new Monolog\Logger('name');

$log->pushHandler(new DiscordHandler\DiscordHandler([
    'https://discordapp.com/api/webhooks/xxx/yyy',
    'https://discordapp.com/api/webhooks/xxx/yyy',
], 'DEBUG'));

Laravel

If you'd like to use this in Laravel, you need to create a custom logging channel inside config/logging.php assuming you're using Laravel 5.6.

'channels' => [
    'custom' => [
        'driver' => 'custom',
        'url' => 'https://discordapp.com/api/webhooks/xxx/yyy',
        'via' => App\Logging\CreateDiscordLogger::class,
        'level' => 'error',
    ],
],

Afterwards create the App\Logging\CreateDiscordLogger class.

<?php

namespace App\Logging;

use Monolog\Logger;
use DiscordHandler\DiscordHandler;

class CreateDiscordLogger
{
    /**
     * Create a custom Discord Monolog instance.
     *
     * @param  array  $config
     * @return \Monolog\Logger
     */
    public function __invoke(array $config)
    {
        $log = new Logger('mylogger');
        $log->pushHandler(new DiscordHandler([$config['url']], $config['level']));

        return $log;
    }
}

This package has no built-in rate limiting so it's recommended to set the log level to ~warning/error to avoid exceeding the rate limit.