php - 如何在 laravel 中广播错误和警告?
问题描述
我正在使用 Laravel 5.6 并socket.io
在我的应用程序的管理面板中广播消息。
errors and warnings
在我的应用程序中,需要在将所有生成的内容登录laravel logs files
到管理员之前广播所有生成的内容。
我只是想知道如何实现此功能。由于我是 Laravel 框架的新手,还有哪些其他简单的方法。
这是我到目前为止所做的:
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ErrorBroadcasting implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $errorContent;
public function __construct($errorContent)
{
$this->errorContent = $errorContent;
}
public function broadcastOn()
{
return new PresenceChannel('error-broadcasting-channel');
}
}
这就是我在刀片中消耗通道的方式。
<script>
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
});
var buyer = Echo.channel(`error-broadcasting-channel`);
buyer.listen("ErrorBroadcasting", e => {
$('#error_container').append("<div class=\"row chat-snippet\">\n" +
" <div class=\"col-lg-12\">\n" +
" <small>Buyer:</small>\n" +
" <p>"+e.errorContent+"</p>\n" +
" </div>\n" +
" </div>");
});
</script>
这就是我修改 app\Exceptions\Handler.php 的方式
public function report(Exception $exception)
{
if (config('app.mail_exception') && $this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
broadcast(new ErrorBroadcasting($exception))->toOthers();
parent::report($exception);
}
解决方案
在你的
应用程序/异常/Handler.php
在这个类里面的渲染方法中你可以
public function report(Exception $exception)
{
broadcast($yourEvent)
parent::report($exception);
}
推荐阅读
- xpath - xPath:您可以仅选择具有特定数量的特定子节点的节点吗?
- android - 在 Android 中使用“ulaw”编码生成 .wav 音频文件
- java - 非线程安全类
- flutter - 如何覆盖 Flutter TabBar 选项卡以用作 FloatingActionButton?
- django - 如何解决Django中两个模块相互导入的问题
- python-3.x - 如何有效地将一个巨大的参数传递给函数?
- jquery - 停止阿贾克斯,如果?(使用多个值自动完成)
- sql - 将 yyyymm 日期转换为 yyyyqn SQL
- asp.net - Azure CDN 正确 URL 指向错误的资源
- c# - 尝试在 Manjaro 上使用 Monodevelop 时遇到的问题