
這個 Laravel 模組簡化了向 Telegram 發送日誌和錯誤訊息。 它非常適合需要簡單日誌記錄解決方案的小型專案。 雖然存在更高級的選項,但此模組優先考慮易於設定和配置。
模組設定
建立一個 Telegram 機器人: 產生一個機器人並取得其令牌。
建立一個 Telegram 群組: 建立一個群組,啟用“主題”,並將您的機器人新增為管理員。
設定.env:將機器人的令牌和群組ID加入您的.env檔案:
<code>TG_LOGGER_TOKEN="your_bot_token" TG_LOGGER_CHAT_ID="your_group_id"</code>
<code class="language-bash">composer require prog-time/tg-logger</code>
<code class="language-bash">php artisan vendor:publish --tag=config</code>
config/tg-logger.php:使用您的設定填滿設定檔:<code class="language-php">return [
'token' => env('TG_LOGGER_TOKEN'),
'chat_id' => env('TG_LOGGER_CHAT_ID'),
'topics' => [
[
'name' => 'Debug messages',
'icon_color' => '9367192',
'level' => 'debug',
],
[
'name' => 'Cron tasks',
'icon_color' => '9367192',
'level' => 'crone',
],
[
'name' => 'Errors',
'icon_color' => '9367192',
'level' => 'error, notice, warning, emergency',
]
]
];</code>tg-logger.php 檔案使用以下參數:
token:您的 Telegram 機器人代幣。 chat_id:您的 Telegram 群組 ID。 topics:定義日誌主題名稱、圖示顏色和關聯日誌等級的陣列。 <code class="language-bash">php artisan tglogger:create-topics</code>
這將覆蓋 tg-logger.php 並添加主題 ID。
使用 TgLogger 模組
A.處理系統錯誤:
修改您的 config/logging.php 檔案以使用模組的處理程序:
<code class="language-php">'channels' => [
...
'telegram' => [
'driver' => 'monolog',
'handler' => ProgTime\TgLogger\TgHandler::class,
'formatter' => ProgTime\TgLogger\TgFormatter::class,
'level' => 'debug',
],
...
],</code>然後,在您的 LOG_CHANNEL=telegram 檔案中設定 .env。
B.直接發送訊息:
使用TgLogger類別直接傳送訊息:
<code class="language-php">TgLogger::sendLog('Your message', 'level');</code>歡迎您在 GitHub 上提供回饋和貢獻!
以上是將日誌發送到 Telegram。 Laravel 模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!