proglab/monolog-discord-handler

一个简单的Monolog处理器,用于支持Discord webhook

v1.0.0 2023-03-01 18:06 UTC

This package is auto-updated.

Last update: 2024-09-29 21:23:37 UTC


README

Latest Stable Version Total Downloads License Monthly Downloads

一个简单的Monolog处理器,用于支持Discord webhook

依赖关系

  • PHP >= 7.2
  • Monolog >= 1.3

如果您想使用这个库与低于7.2版本的PHP,请安装0.3之前的版本

1. 安装

通过composer轻松安装。还不知道composer是什么?请在这里了解 这里

composer require lefuturiste/monolog-discord-handler

2. 使用方法

将此处理器推送到您的Monolog实例

单个webhook URL

<?php
require 'vendor/autoload.php';

$log = new Monolog\Logger('your name');

$log->pushHandler(new DiscordHandler\DiscordHandler('https://discordapp.com/api/webhooks/xxx/yyy', 'name', 'subname', 'DEBUG'));

多个webhook URL

<?php
require 'vendor/autoload.php';

$log = new Monolog\Logger('your name');

$log->pushHandler(new DiscordHandler\DiscordHandler([
  'https://discordapp.com/api/webhooks/xxx/yyy',
  'https://discordapp.com/api/webhooks/xxx/yyy'
], 'name', 'subname', 'DEBUG'));

使用配置

您可以自定义默认消息和日期时间格式。

<?php
require 'vendor/autoload.php';

$log = new Monolog\Logger('name');

$handler = new DiscordHandler\DiscordHandler('https://discordapp.com/api/webhooks/xxx/yyy', 'name', 'subname', 'DEBUG');

$handler->getConfig()
    ->setMultiMsg(true)
    ->setMaxMessageLength(2000) // at least 50 characters
    ->setDatetimeFormat('Y/m/d H:i')
    ->setTemplate("{datetime} {name}: {message}");

// or you can create another Config instance and replace it:
$otherConfig = new DiscordHandler\Config();
$otherConfig->setWebHooks([
  'https://discordapp.com/api/webhooks/xxx/yyy', 
  'https://discordapp.com/api/webhooks/xxx/yyy'
]);

$handler->setConfig($otherConfig);

$log->pushHandler($handler);