Laravel记录操作日志实现方法,并记录操作日志到数据表

2021-04-26   阅读:3141   分类:后端    标签: Laravel

image.png

Laravel记录操作日志,并且记录到数据表中。所有到的技术,路由、中间件、模型、数据表

步骤1、创建数据表:用于存放日志

CREATE TABLE `sys_admin_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `admin_name` varchar(255) DEFAULT NULL COMMENT '账号',
  `created_at` int(12) DEFAULT NULL COMMENT '操作时间',
  `ip` varchar(200) CHARACTER SET utf8 DEFAULT NULL COMMENT 'ip',
  `content` text COMMENT '日志',
  `admin_id` int(11) DEFAULT NULL COMMENT '账号id',
  `path` varchar(255) DEFAULT NULL COMMENT '操作路由',
  `method` varchar(255) DEFAULT NULL COMMENT '操作方法',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='操作日志表';

步骤2、创建模型OperationLog.php关联数据表

<?php

namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OperationLog extends Model
{
    use HasFactory;


    //关联数据表
    public $table = 'sys_admin_log';
    //关联主键
    public $primaryKey ='id';
    //允许批量操作的字段
    public $guarded=[];
    //是否维护created_at和updated_at字段
    public $timestamps = false;

    //查询处理管理员数据列表
    public static function adminLogList($where,$limit,$columns,$pageName,$page){
        $admin_log_list=OperationLog::where($where)
            ->orderBy('id', 'desc')
            ->paginate($limit, $columns, $pageName, $page)
            ->toArray();
        if(!empty($admin_log_list['data'])){
            foreach ($admin_log_list['data'] as $k=>$v){
                $admin_log_list['data'][$k]['created_at']=date('Y-m-d H:i:s',intval($v['created_at']));
            }
        }
        return $admin_log_list;
    }
}
?>

步骤3、创建中间件OperationLog.php文件:用于实现记录操作

<?php

namespace App\Http\Middleware;

use App\Http\Response\ApiErrDesc;
use App\Http\Response\ResponseJson;
use App\Models\Admin\Admin;
use Closure;
use Illuminate\Http\Request;

class OperationLog
{
    use ResponseJson; //统一返回json
    /**
     * Handle an incoming request.
     *
     * 操作日志中间件
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $input = $request->all(); //操作的内容
        $path = $request->path();  //操作的路由
        $method = $request->method();  //操作的方法
        $ip = $request->ip();  //操作的IP
        $admin_id = session()->get('admin_id');  //操作人(要自己获取)
        if(!$request->isMethod('get')){
            self::writeLog($admin_id,$input,$path,$method,$ip);
        }


        return $next($request);
    }
    public function writeLog($admin_id,$input,$path,$method,$ip){

        $user = Admin::where('id',$admin_id)->first();
        if($user) {
            $user_id = $user->id;
            $admin_name = $user->admin_name;
        }else{
            return $this->jsonData(ApiErrDesc::ILLEGAL[0],ApiErrDesc::ILLEGAL[1]);
        }

        $log = new \App\Models\Admin\OperationLog();
        $log->setAttribute('admin_id', $user_id);
        $log->setAttribute('admin_name', $admin_name);
        $log->setAttribute('path', $path);
        $log->setAttribute('method', $method);
        $log->setAttribute('ip', $ip);
        $log->setAttribute('content', json_encode($input, JSON_UNESCAPED_UNICODE));
        $log->setAttribute('created_at', time());
        $log->save();
    }
}
?>

步骤4、注册路由中间件

在路由文件web.php添加中间件:->middleware(['OperationLog'])

打开Kernel.php文件,注册中间件:

<?php
protected $routeMiddleware = [
   .......
    'OperationLog' => \App\Http\Middleware\OperationLog::class,

];
?>

至此可以看到数据入库文件,包括账号,账号id,用户操作时间、操作路由地址、操作方法method、操作表字段数据等。

image.png

【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/667.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×