jeremykenedy/laravel-exception-notifier

Laravel异常通知器会将错误邮件及堆栈跟踪发送到指定的收件人。

资助包维护!
jeremykenedy
Patreon

安装次数: 206 980

依赖: 1

建议者: 0

安全: 0

星标: 128

关注者: 7

分支: 29

开放问题: 0

类型:package

V4.0.0 2024-01-21 01:12 UTC

This package is auto-updated.

Last update: 2024-09-21 02:28:12 UTC


README

Total Downloads Latest Stable Version Build Status StyleCI Scrutinizer Code Quality Code Intelligence Status MadeWithLaravel.com shield License: MIT

目录

关于

Laravel异常通知器会将错误邮件及堆栈跟踪发送到指定的收件人。 此包 包含所有必要的特性、视图、配置和邮件发送器,以便在应用程序异常时进行邮件通知。您可以根据环境自定义发送给谁、抄送谁、密送谁,以及启用/禁用自定义主题或默认主题。适用于 Laravel 5.2、5.3、5.4、5.5、5.6、5.7、5.8、6、7、8、9 和 10。

在客户端报告之前,获取错误并修复它们,这就是为什么存在它的原因!

要求

安装说明

  1. 在终端中从项目根目录运行

    Laravel 9-10 使用

        composer require jeremykenedy/laravel-exception-notifier

    Laravel 7-8 使用

        composer require jeremykenedy/laravel-exception-notifier:2.2.0

    Laravel 6 及以下使用

        composer require jeremykenedy/laravel-exception-notifier:1.2.0
  2. 注册包

  • Laravel 5.5 及以上使用包自动发现功能,无需编辑 config/app.php 文件。

  • Laravel 5.4 及以下在 config/app.php 下的 providers 中注册包,如下所示

    jeremykenedy\laravelexceptionnotifier\LaravelExceptionNotifier::class,
  1. 从项目根目录运行以下命令以发布包的视图、邮件发送器和配置文件
    php artisan vendor:publish --tag=laravelexceptionnotifier

注意:如果您从旧版本的此包升级到 Laravel 9 或 10,则需要使用以下命令重新发布资产

    php artisan vendor:publish --force --tag=laravelexceptionnotifier
  1. App\Exceptions\Handler.php 中,在开头包含以下附加类

Laravel 9 及以上使用

    use App\Mail\ExceptionOccurred;
    use Illuminate\Support\Facades\Log;
    use Illuminate\Support\Facades\Mail;
    use Throwable;

Laravel 8 及以下使用

    use App\Mail\ExceptionOccured;
    use Illuminate\Support\Facades\Log;
    use Mail;
    use Symfony\Component\Debug\Exception\FlattenException;
    use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
  1. 更新 App\Exceptions\Handler.php

Laravel 9 及以上

添加 sendEmail() 方法
    /**
     * Sends an email upon exception.
     */
    public function sendEmail(Throwable $exception): void
    {
        try {
            $content = [
                'message' => $exception->getMessage(),
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
                'trace' => $exception->getTrace(),
                'url' => request()->url(),
                'body' => request()->all(),
                'ip' => request()->ip(),
            ];

            Mail::send(new ExceptionOccurred($content));
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }
添加或更新 register() 方法
    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            $enableEmailExceptions = config('exceptions.emailExceptionEnabled');

            if ($enableEmailExceptions) {
                $this->sendEmail($e);
            }
        });
    }

Laravel 8 及以下

用以下内容替换 report() 方法
    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param \Throwable $exception
     *
     * @return void
     */
    public function report(Throwable $exception)
    {
        $enableEmailExceptions = config('exceptions.emailExceptionEnabled');

        if ($enableEmailExceptions === '') {
            $enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
        }

        if ($enableEmailExceptions && $this->shouldReport($exception)) {
            $this->sendEmail($exception);
        }

        parent::report($exception);
    }
添加方法 sendEmail()
    /**
     * Sends an email upon exception.
     *
     * @param \Throwable $exception
     *
     * @return void
     */
    public function sendEmail(Throwable $exception)
    {
        try {
            $e = FlattenException::create($exception);
            $handler = new SymfonyExceptionHandler();
            $html = $handler->getHtml($e);

            Mail::send(new ExceptionOccured($html));
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }
  1. .env 文件中配置您的邮件设置。

  2. 将以下(可选)设置添加到您的 .env 文件中,并输入您的设置

    • 注意:这些设置的默认值位于 config/exception.php
        EMAIL_EXCEPTION_ENABLED=false
        EMAIL_EXCEPTION_FROM="${MAIL_FROM_ADDRESS}"
        EMAIL_EXCEPTION_TO='email1@gmail.com, email2@gmail.com'
        EMAIL_EXCEPTION_CC=''
        EMAIL_EXCEPTION_BCC=''
        EMAIL_EXCEPTION_SUBJECT=''

屏幕截图

Email Notification

文件树

└── laravel-exception-notifier
    ├── .gitignore
    ├── LICENSE
    ├── composer.json
    ├── readme.md
    └── src
        ├── .env.example
        ├── App
        │   ├── Mail
        │   │   └── ExceptionOccurred.php
        │   └── Traits
        │       └── ExceptionNotificationHandlerTrait.php
        ├── LaravelExceptionNotifier.php
        ├── config
        │   └── exceptions.php
        └── resources
            └── views
                └── emails
                    └── exception.blade.php
  • 可以使用 brew 安装 tree 命令:brew install tree
  • 使用以下命令生成文件树:tree -a -I '.git|node_modules|vendor|storage|tests'

许可协议

Laravel-Exception-Notifier | 一个 Laravel 异常邮件通知包,是开源软件,许可证为 MIT 许可