deslynx/alert-bundle

发送警报电子邮件和专业的Monolog处理器

安装: 8

依赖: 0

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放性问题: 0

类型:symfony-bundle

v1.1.1 2024-07-18 08:51 UTC

This package is auto-updated.

Last update: 2024-09-18 09:45:28 UTC


README

Latest stable version License PHPStan Enabled PHPStan Level max

安装

确保Composer已全局安装,具体请参阅Composer文档中的安装章节

使用Symfony Flex的应用程序

打开命令行,进入您的项目目录,并执行以下命令

composer require deslynx/alert-bundle

未使用Symfony Flex的应用程序

步骤 1:下载Bundle

打开命令行,进入您的项目目录,并执行以下命令以下载此Bundle的最新稳定版本

composer require deslynx/alert-bundle

步骤 2:启用Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的Bundle列表中来启用此Bundle

// config/bundles.php

return [
    // ...
    DesLynx\AlertBundle\DesLynxAlertBundle::class => ['all' => true],
];

配置

# /config/packages/deslynx_alert.yaml
deslynx_alert:

    # The sender of the email, like "Name <email@address.com>" or a simple email.
    from: ~ # Required

    # A list of always added recipients for the email, like "Name <email@address.com>" or a simple email.
    to: [] # Required

    # Your project name. Included in the email subject
    projectName: 'My project'

    # If you want to use a custom email transport set this to the name of the transport.
    transport: null

# If you need another configuration for specific environments you can overwrite some configs
#when@dev:
#  deslynx_alert:
#    from: ~
#    to: []
#    projectName: 'My project'
#    transport: null

重要提示

此Bundle依赖于Symfony Mailer组件来发送电子邮件,因此请确保已配置它。

使用(服务)

此Bundle提供用于发送警报电子邮件的单个服务,您可以通过使用AlertHelper类型提示来自动注入。

// src/Controller/SomeController.php

use DesLynx\AlertBundle\Service\AlertHelper;
//...

class SomeController
{
    public function index(AlertHelper $alertHelper) {
        $alertHelper->sendAlert(
            'A significant subject.',
            'A significant message. Either text or HTML.',
            false, // Optional. Set to true if the message is HTML
            ['email1@address.com', 'email2@address.com' /*, ...*/] // Optional. A list of recipients to add in cc in addition to the globally defined recipients (see configuration)
         );
    }    
}

使用(Monolog处理器)

此Bundle提供使用AlertHelper服务的Monolog处理器。它允许在项目中发生每个关键错误时发送带有完整日志堆栈的警报电子邮件。

要启用此功能,如果尚未安装,则要求安装MonologBundle,然后只需将其添加到您的monolog配置中即可。

# config/packages/monolog.yaml
monolog:
  handlers:
    # ...
    deslynx_critical:
      type: fingers_crossed
      action_level: critical
      handler: deslynx_deduplicated
    deslynx_deduplicated:
      type: deduplication
      handler: deslynx_alert_mailer
    deslynx_alert_mailer:
      type: service
      id: DesLynx\AlertBundle\Monolog\Handler\AlertMailerHandler
      level: debug