arhx/telegram-log-channel
Laravel log channel that sends Monolog messages to a Telegram chat via bot token and chat ID. Configure via .env or logging.php, add to your logging stack, and it safely falls back to a NullHandler when unset. Includes optional queued job failure alerts.
Install the Package
composer require arhx/telegram-log-channel
Publish Configuration
php artisan vendor:publish --provider="Arhx\TelegramLogChannel\TelegramLogChannelServiceProvider"
Edit .env with your Telegram bot token and chat ID:
TELEGRAM_BOT_TOKEN=your_bot_token_here
TELEGRAM_CHAT_ID=your_chat_id_here
Configure config/logging.php
Add the channel under channels:
'telegram' => [
'driver' => 'telegram',
'bot_token' => env('TELEGRAM_BOT_TOKEN'),
'chat_id' => env('TELEGRAM_CHAT_ID'),
'level' => env('TELEGRAM_LOG_LEVEL', 'debug'),
'formatted' => env('TELEGRAM_LOG_FORMATTED', true),
],
Register the Channel
Add 'telegram' to your stack in logging.php:
'stack' => env('LOG_STACK', 'single'),
'channels' => [
'telegram' => [...],
],
Test It Log a test message:
\Log::channel('telegram')->info('Test message from Laravel!');
debug, info, warning, error, critical).
\Log::channel('telegram')->error('Database connection failed!');
\Log::channel('telegram')->debug('User login attempt', [
'user_id' => 123,
'ip' => request()->ip(),
]);
single, daily) in logging.php:
'stack' => [
'channels' => ['stack', 'telegram'],
],
if checks to route logs dynamically:
if (app()->environment('production')) {
\Log::channel('telegram')->warning('Production alert!');
}
formatted: false in config to send raw log data.\Log::channel('telegram')->info('*Bold text* and `code`', [], [
'format' => 'markdown',
]);
\Log::channel('telegram')->useQueue(true)->info('Async log message');
Ensure QUEUE_CONNECTION is configured in .env.Bot Token/Chat ID Mismatch
TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID are correct.Rate Limits
warning/error for critical alerts only.Queue Delays
failed_jobs table for errors.Formatting Issues
formatted: true, ensure log data is JSON-serializable._, *) in Markdown mode.formatted: false to debug payloads.try {
\Log::channel('telegram')->error('Test error');
} catch (\Exception $e) {
\Log::channel('single')->error('Telegram log failed: ' . $e->getMessage());
}
Custom Formatting Override the formatter via a service provider:
$this->app->bind(\Arhx\TelegramLogChannel\Formatter\FormatterInterface::class, function () {
return new CustomFormatter();
});
Filtering Logs Extend the channel to filter logs before sending:
\Log::channel('telegram')->extend('filtered', function ($app) {
return tap(new \Arhx\TelegramLogChannel\TelegramChannel($app), function ($channel) {
$channel->setFilter(function ($level, $message, $context) {
return str_contains($message, 'critical');
});
});
});
Multiple Chats Use a single bot token with multiple chat IDs by extending the channel:
\Log::channel('telegram')->extend('multi', function ($app) {
return new \Arhx\TelegramLogChannel\TelegramChannel($app, [
'chat_ids' => [123, 456], // Array of chat IDs
]);
});
App\Exceptions\Handler:
public function report(Throwable $exception) {
\Log::channel('telegram')->error('Uncaught exception', [
'exception' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);
}
local:
'telegram' => [
'driver' => 'telegram',
'enabled' => app()->environment('production'),
],
How can I help you explore Laravel packages today?