操作日志记录:

日志表:

 

接下来需要在配置封装日志(方便其他模块直接调用)

 

 

 

 

------------------------------------------------------------------举例:注册功能添加日志 --------------------------------------------------------------------

try_catch方法抛异常

try_catch作用:1.如果在注册的时候他没有注册成功,正常来说提示就是注册失败,在这个时候日志里就会有一条对应注册失败错误日志

                          2.try里面写的是成功的数据,catch里面处理异常

使用事务:

beginTransaction()开启事务

rollBack()回滚事务

commit()提交事务

 

 

注册:日志记录代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public function register(ManageUserRequest $request)
    {
        $manage = [
            'password' => $request->password,
            "username" => $request->username,
            'phone' => $request->phone,
            'role_id' => $request->role_id,
            'created_at' => date("Y-m-d H:i:s")
        ];
        if (isset($manage['password']) && !empty($manage['password'])) {
            $manage['password'] = Hash::make($manage['password']);
        }
 
        DB::beginTransaction();
 
        try {
            if (ManageUserModel::query()->create($manage)) {
                //记录日志:数据成功
                $data = [
                    'username' => \Illuminate\Support\Facades\Cache::get('username'),
                    'created_at' => date("Y-m-d H:i:s"),
                    'operation_desc' => '用户注册成功',
                    'controller' => 'ManageUserController',
                    'method' => 'register',
                    'ip' => gethostbyname('')//获取ip地址
                ];
                $this->saveLog($data);
                DB::commit();
                return $this->success($manage, "用户注册成功");
            } else {
                //记录日志:数据失败
                $data = [
                    'username' => \Illuminate\Support\Facades\Cache::get('username'),
                    'created_at' => date("Y-m-d H:i:s"),
                    'operation_desc' => '用户注册失败',
                    'controller' => 'ManageUserController',
                    'method' => 'register',
                    'ip' => gethostbyname('') //获取ip地址
                ];
                $this->saveLog($data);
                return $this->failed("用户注册失败");
            }
 
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->failed($e->getMessage());
        }
    }