codelikesuraj / lograh-php
一个轻量级的日志记录器,将日志记录到Telegram。
v1.0.0
2024-02-22 21:04 UTC
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2024-09-20 14:04:38 UTC
README
LograhPHP会将您的异常发送到Telegram聊天、频道或群组。
需求
- PHP 8.0或更高版本。
- 启用php_curl扩展。
- Telegram机器人的访问令牌和聊天/频道/群组ID。通过https://telegram.me/BotFather生成Telegram机器人。
安装
$ composer require codelikesuraj/lograh-php
基本用法
示例代码
<?php use Codelikesuraj\LograhPHP\Logger; // initialize logger with your Telegram bot credentials $logger = new Logger( appName: "unique_name_to_identify_your_app", botToken: "api_key_generated_from_your_telegram_bot", chatId: "id_of_your_telegram_chat_or_channel_or_group" ); try { // code that may generate an exception ... } catch (\Throwable $exception) { // optional list of exceptions to be ignored $logger->ignore([ExceptionA::class, ExceptionB::class]); // send exception to Telegram using any // of the following methods $logger->reportAsText($exception); $logger->reportAsJson($exception); $logger->reportAsJsonWithStackTrace($exception); // further processing ... }
发送到Telegram的示例响应消息
以TEXT格式报告
app: 识别您应用的唯一名称
timestamp: 0000-00-00 00:00:00 +0000
"summary": 未捕获的异常: 'exception_name',信息: 'exception_message',在 /path/to/folder/file.php:#
以JSON格式报告
{ "app": "unique_name_to_identify_your_app", "timestamp": "0000-00-00 00:00:00 +0000", "summary": "Uncaught exception: 'exception_name' with message 'exception_message' in \/path\/to\/folder\/file.php:#", }
以带有堆栈跟踪的JSON格式报告
{ "app": "unique_name_to_identify_your_app", "timestamp": "0000-00-00 00:00:00 +0000", "summary": "Uncaught exception: 'exception_name' with message 'exception_message' in \/path\/to\/folder\/file.php:#", "stack trace": [ "#0 {main}" ] }
作者
Abdulbaki Suraj - http://twitter.com/fliplikesuraj
许可证
LograhPHP遵循MIT许可证。