Skip to content

ipagdevs/monolog-discord-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monolog Discord Handler

Latest Version on Packagist Total Downloads License PHP Version Build Status

Send Monolog logs directly to a Discord webhook using rich embeds, automatic formatting, and safe value normalization.


Installation

composer require ipagdevs/monolog-discord-handler

Usage (Plain Monolog)

use Monolog\Logger;
use Monolog\Level;
use IpagDevs\Logging\DiscordHandler;

$logger = new Logger('app');

$logger->pushHandler(
    new DiscordHandler(
        webhook_url: 'https://discord.com/api/webhooks/xxx',
        level: Level::Debug
    )
);

$logger->error('System error occurred', [
    'user_id' => 123,
    'exception' => new RuntimeException('Critical failure'),
]);

How it works

The handler automatically:

  • Converts logs into Discord embeds

  • Respects Discord field and message limits

  • Normalizes values:

    • array/object → pretty JSON
    • Throwable → formatted stacktrace block
    • Closure → callable[closure]
    • string callables → callable:method
  • Safely truncates large payloads

  • Prevents application crashes (fail-safe HTTP handling)


Laravel Usage

In config/logging.php:

'channels' => [
    'discord' => [
        'driver' => 'monolog',
        'handler' => IpagDevs\Logging\DiscordHandler::class,
        'with' => [
            'webhook_url' => env('DISCORD_WEBHOOK_URL'),
        ],
        'level' => 'debug',
    ],
],

In .env:

DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/xxx

Example

Log::error('Payment failed', [
    'order_id' => 999,
    'user' => [
        'id' => 1,
        'name' => 'Lucas',
    ],
    'exception' => new RuntimeException('Gateway timeout'),
]);

This will be sent to Discord as an embed with:

  • log level as title
  • message as description
  • context automatically converted into fields

Safety

This handler is designed to be safe by default:

  • never throws exceptions outside the handler
  • never blocks application execution
  • ignores HTTP failures silently

License

The MIT License (MIT). Please see License File for more information.

About

Monolog handler that sends application logs to Discord via webhooks, with support for embedded messages, automatic value normalization, and safe failure handling.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages