Laravel框架实现后台操作日志功能(provider服务、event事件、listen监听、observe观察者)(一)

后台操作日志如何更优雅更效率?

我们先看看大部分写后台日志是怎样的? 有一个操作日志类 然后当有操作的时候去调用 这么写能不能用实现功能?能实现,但是作为技术不该遇到问题只想着实现,而是做到如何更好的实现,做一个可扩展,减少冗余代码,高效、稳定的设计能力。 所以废话少说,我们上代码,GO!

首先创建用户存储后台操作日志的表

php artisan make:migration admin_log

在生成好的文件中up方法中加入如下代码

		# 注:这知识一个演示表,实际的表要更严谨请自行扩展
		# 第一个参数为数据表名,第二个参数为一个 闭包 ,此闭包会接收一个用于定义新数据表的 Blueprint 对象
        /**
         * integer:int 类型
         * string: varchar 类型
         * tinyInteger :tinyint 类型
         */
        Schema::create('admin_log', function (Blueprint $table) {
            $table->increments('id', 11);
            $table->integer('admin_user_id');
            $table->string('path', 50);
            $table->string('input', 255);
            $table->tinyInteger('type');
            $table->string('change_data', 255);
            $table->string('message', 50);
            $table->tinyInteger('data_type');
            $table->string('relevance_id', 50);
            $table->tinyInteger('relevance_type');
            $table->timestamps(); # 加入 created_at 和 updated_at 字段
        });

注册事件以及监听器

在 app/Providers/目录下的EventServiceProvider.php中注册事件监听器映射关系:

		'App\Events\DefaultLog' => [
            'App\Listeners\DefaultLogListener',
        ],
php artisan event:generate

这样app/Events和app/Listensers目录下生成DefaultLog.DefaultLogListener.php文件

在Event/DefaultLog.php中加入以下代码

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class DefaultLog extends Event
{
    use SerializesModels;

    public $title;

    public $content;

    public $input;

    public $userId;

    public $path;

    public $type;

    public $modelId;


    /**
     * Create a new event instance.
     *
     * DefaultLog constructor.
     * @param string $title
     * @param string $content
     * @param $input
     * @param int $userId
     * @param string $path
     * @param int $type
     * @param int $modelId
     */
    public function __construct(string $title, string $content, $input, $userId = 0, $path = '', $type = 0, $modelId = 0)
    {
        $this->title = $title;
        $this->content = $content;
        $this->input = $input;
        $this->userId = $userId;
        $this->path = $path;
        $this->type = $type;
        $this->modelId = $modelId;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

先创建一个model

php artisan make:model Models/AdminLog

在Listeners handle 中加入以下代码

	<?php

namespace App\Listeners;

use App\Events\DefaultLog;
use App\Models\AdminLog;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class DefaultLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  DefaultLog  $event
     * @return void
     */
    public function handle(DefaultLog $event)
    {
        AdminLog::create([
            'admin_user_id' => $event->userId,
            'path' => $event->path,
            'message' => $event->title,
            'change_data' => $event->content,
            'input' => $event->input,
            'type' => $event->type,
            'created_time' => time(),
            'data_type' => 1,
            'relevance_id' => $event->modelId,
            //'created_ip' => get_client_ip(),
        ]);
    }
}

**越写越觉得想表达的更多,本章分成4个部分,(一)(二)前两个部分是实现,(三)讲原理和自己的理解,(四)让这套设计不只是为日志服务,加入Redis缓存,让读服务更快,减少服务器压力。 这是小破站备案上线以来第一个周末,也是第一期技术分享,希望对你有帮助。 Oss **

Elkan的小破站
请先登录后发表评论
  • latest comments
  • 总共0条评论