iqbalatma/laravel-log-telegram-channel

该软件包最新版本(0.4.0)没有提供许可证信息。

Laravel日志通过Telegram频道

0.4.0 2024-07-12 14:39 UTC

This package is auto-updated.

Last update: 2024-09-12 14:59:40 UTC


README

这是一个自定义通道的日志处理器,用于通过Telegram机器人发送日志。Telegram机器人会将消息发送到频道,并且您可以指定日志级别。

重要

由于Telegram机器人对发送消息有数量限制,请谨慎使用此包。我的建议是使用它来记录高级别的日志,如紧急或关键。

如何安装

您可以通过Composer安装此包

composer require iqbalatma/laravel-log-telegram-channel

如何发布配置

您可以通过以下命令发布配置文件

php artisan vendor:publish --provider="Iqbalatma\LaravelLogTelegramChannel\LogTelegramChannelServiceProvider"

如何添加日志通道

在开始使用此日志处理器之前,您必须先在日志配置文件中添加此处理器的通道。打开config/logging.php并添加以下通道

<?php


return [

  #this is default channel when logging if are not specifying the channel
  'default' => 'stackk',
  'channels' => [
      #when you choose channel with driver stack, the log will send into multiple channel
      #in this case, you will send into 2 channels, single and telegram.
      'stack' => [
            'driver' => 'stack',
            'channels' => ["single", "telegram"],
            'ignore_exceptions' => false,
      ],

      'single' => [
          'driver' => 'single',
          'path' => storage_path('logs/laravel.log'),
          'level' => env('LOG_LEVEL', 'debug'),
          'replace_placeholders' => true,
      ],

      #you can custom level log to log level on that level or higher
      'telegram' => [
            'driver' => 'custom',
            'via' => \Iqbalatma\LaravelLogTelegramChannel\Logger::class,
            'level' => 'debug'
      ],
];

Laravel日志Telegram频道配置文件

此配置文件用于设置凭证和日志处理器行为

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Telegram Bot Host
    |--------------------------------------------------------------------------
    |
    | This is telegram bot host. You can find this host information at telegram
    | bot documentation https://core.telegram.org/bots/api
    |
    */
    "host" => env("LOG_TELEGRAM_HOST", "https://api.telegram.org"),


    /*
    |--------------------------------------------------------------------------
    | Telegram Bot Token
    |--------------------------------------------------------------------------
    |
    | This is token for authorization. You can get this token when create
    | telegram bot via BotFather. You need to add prefix bot on you generated
    | token. If your token like this 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
    | then you token value for this configuration would be
    | bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
    |
    */
    "token" => env("LOG_TELEGRAM_TOKEN", null),


    /*
    |--------------------------------------------------------------------------
    | Telegram Channel Id
    |--------------------------------------------------------------------------
    |
    | This is channel id of target room chat channel. You can get this value
    | via username of the channel with format @channelusername. If your channel
    | is private, try to change channel into public and then send message via
    | postman to get response information of channel id. The value would be like
    | this -1002017173213. If you are using private channel, please use this id
    | format.
    |
    */
    "channel_id" => env("LOG_TELEGRAM_CHANNEL_ID", null),


    /*
    |--------------------------------------------------------------------------
    | Truncate Message
    |--------------------------------------------------------------------------
    |
    | When you send message via bot telegram, the message has length limit of
    | string. So you need to tell is the log message will be truncated, or
    | the message will send multiple times. If you set false, the message will
    | not truncated and send multiple times.
    |
    */
    "is_truncate_message" => env("LOG_TELEGRAM_IS_TRUNCATE_MESSAGE", false),



    /*
    |--------------------------------------------------------------------------
    | Fallback Channel
    |--------------------------------------------------------------------------
    |
    | There are conditions when this library got some failure and throw exception.
    | In this case, we will catch that exception and send log to this specific
    | channel to prevent infinity loop.
    |
    */
    "fallback_channel" => env("LOG_TELEGRAM_FALLBACK_CHANNEL", "single"),
];