syntech/notifier

一个用于在错误发生时发送电子邮件通知的Laravel包

1.0.4 2024-06-24 20:15 UTC

This package is auto-updated.

Last update: 2024-09-24 20:45:35 UTC


README

Laravel Error Notifier 是一个Laravel包,当您的Laravel应用程序发生错误时,它会发送电子邮件通知。这有助于您及时监控和解决问题。

安装

步骤1:通过Composer安装

您可以通过Composer安装此包。在Laravel应用程序根目录中运行以下命令

composer require syntech/notifier

步骤2:发布配置文件

Publish the package's configuration file to customize the email recipient for error notifications:
php artisan vendor:publish --provider="Syntech\Notifier\LaravelErrorNotifierServiceProvider"

该命令将在config/error-notifier.php处创建一个配置文件。

步骤3:配置电子邮件接收者

打开新创建的配置文件config/error-notifier.php,并设置应将错误通知发送到的电子邮件地址

return [
  'email' => env('ERROR_NOTIFIER_EMAIL', 'admin@example.com'),
];

确保在.env文件中设置了ERROR_NOTIFIER_EMAIL环境变量

ERROR_NOTIFIER_EMAIL=your-email@example.com

步骤4:配置邮件设置

确保您的Laravel应用程序的邮件设置在.env文件中正确配置。以下是一个使用SMTP的示例配置

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME="${APP_NAME}"

将这些建议设置替换为您的实际邮件服务器详细信息。

步骤5:更新日志配置

将自定义日志通道添加到您的应用程序的config/logging.php文件中

use SyntechNotifier\LaravelErrorNotifier\Logging\CustomHandler;

return [
  'channels' => [
      // other channels...

      'email' => [
          'driver' => 'custom',
          'via' => CustomHandler::class,
          'level' => 'error',
      ],
  ],
];

步骤6:更新异常处理器

打开app/Exceptions/Handler.php文件,并更新report方法以将错误记录到邮件通道

use Illuminate\Support\Facades\Log;
use Throwable;

public function report(Throwable $exception)
{
  parent::report($exception);

  if ($this->shouldReport($exception)) {
      Log::channel('email')->error('An error occurred', ['exception' => $exception]);
  }
}

使用方法

安装和配置完成后,该包将自动在您的Laravel应用程序中发生错误时发送电子邮件通知。自定义通知电子邮件

如果您需要自定义电子邮件通知,可以修改位于src/Notifications/ErrorOccurred.php的ErrorOccurred通知类。此类定义了当发生错误时发送的电子邮件的内容和结构。示例

以下是一个可能的电子邮件通知示例

主题:您的应用程序发生错误

发生了一个错误

  • 消息:示例错误消息
  • 文件:/path/to/file.php
  • 行:123
  • 堆栈跟踪:#0 /path/to/file.php(123): exampleFunction() #1 {main}

请及时解决这个问题。

贡献

如果您想为此包做出贡献,请按照以下步骤操作

Fork the repository on GitHub.
Create a new branch for your feature or bugfix.
Make your changes and commit them with a descriptive message.
Push your changes to your forked repository.
Submit a pull request to the main repository.

许可证

此包是开源软件,受MIT许可证的许可。支持

如果您遇到任何问题或有任何疑问,请随时在GitHub仓库上打开一个问题。