vsavritsky/monolog-telegram 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vsavritsky/monolog-telegram

Composer 安装命令:

composer require vsavritsky/monolog-telegram

包简介

Monolog handler that sends logs through Telegram bot to any chat in HTML format

README 文档

README

Send your logs through Telegram bot to any chat and make them look fancy!

Features:

  • Continous logging and batch logging support
  • Standard stack traces wrapped in <code></code> tags
  • Automatic splitting of the message when it exceeds maximum limit

Prerequisites

  • Telegram Bot API token - see here to learn how to obtain one
  • ID of the chat to which you want to send the logs - see below

Obtaining chat ID

One of the simplest ways to do that is to interact with the bot in the target chat:

  • private and group chats - send any dummy command
  • channels - post something in it

After interacting visit https://api.telegram.org/botTOKEN/getUpdates (replace TOKEN with your actual bot token), you will be able to find the chat id (chat_id) in the result JSON.

Installation

Install with Composer:

$ composer require jacklul/monolog-telegram

Usage

To use this handler you just have to add it like every other Monolog handler:

require 'vendor/autoload.php';

$api_token = '123456789:teMbvbETojnSG93jDhnynvH8pT28H9TIB1h';    // Bot API token
$chat_id = 987654321;    // Target Chat ID
$level = Logger::ERROR;     // Log level
$bubble = true;     // Bubble up the stack or not
$use_curl = true;    // Use cURL or not? (default: use when available)
$timeout = 10;   // Timeout for API requests
$verify_peer = true;   // Verify SSL certificate or not? (development/debugging)

$logger = new Logger('My project');
$handler = new TelegramHandler($api_token, $chat_id, $level, $bubble, $use_curl, $timeout, $verify_peer);
$handler->setFormatter(new TelegramFormatter());    // Usage of this formatter is optional but recommended if you want better message layout
$logger->pushHandler($handler);

$logger->error('Error!');

To prevent spamming the chat and hitting Telegram's API limits it is advised to use DeduplicationHandler and/or BufferHandler, ideal solution for production use would be:

$handler = new TelegramHandler('TOKEN', 123456789);
$handler->setFormatter(new TelegramFormatter());

// Combine all log entries into one and force batch processing
$handler = new BufferHandler($handler);

// Make sure that particular log stack wasn't sent before
$handler = new DeduplicationHandler($handler);

// Keep collecting logs until ERROR occurs, after that send collected logs to $handler
$handler = new FingersCrossedHandler($handler, new ErrorLevelActivationStrategy(Logger::ERROR));

$logger->pushHandler($handler);

You can customize the formatter:

$html = true;    // Choose whether to send the message in HTMl format
$format = "<b>%level_name%</b> (%channel%) [%date%]\n\n%message%\n\n%context%%extra%";   // Error (My project) [2018-05-01 15:55:15 UTC]
$date_format = 'Y-m-d H:i:s e';       // 2018-05-01 15:55:15 UTC, format must be supported by DateTime::format
$separator = '-';       // Seperation character for batch processing - when empty one empty line is used

$handler->setFormatter(new TelegramFormatter($html, $format, $date_format, $separator));

License

See LICENSE.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固