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、操作表字段数据等。
文章评论(0)