Send Monolog logs directly to a Discord webhook using rich embeds, automatic formatting, and safe value normalization.
composer require ipagdevs/monolog-discord-handleruse 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'),
]);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)
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/xxxLog::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
This handler is designed to be safe by default:
- never throws exceptions outside the handler
- never blocks application execution
- ignores HTTP failures silently
The MIT License (MIT). Please see License File for more information.