| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Services;
- use App\Log;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Request;
- class LogService
- {
- static public function log($method,$type,$description,$id_user=null){
- if(!$id_user){
- $id_user = '';
- $user=auth()->user();
- if($user) $id_user = $user['id'];
- }
- $date = date('Y-m-d H:i:s');
- $log = new Log([
- 'operation'=>$method,
- 'type'=>$type,
- 'description'=>$description,
- 'id_user'=>$id_user,
- 'ip'=>Request::ip(),
- 'created_at' => $date,
- 'updated_at' => $date
- ]);
- Redis::LPUSH('LOGS', $log);
- }
- public static function syncRedisLogs()
- {
- $data = [];
- $length = 0;
- while (Redis::LLEN('LOGS') > 0) {
- $log = Redis::LPOP('LOGS');
- $arr = json_decode($log);
- if ($length + strlen($arr->description) > 1024 * 512) {
- Log::query()->insert($data);
- $length = 0;
- $data = [];
- }
- $length = $length + strlen($arr->description);
- $data[] = [
- 'operation' => $arr->operation,
- 'id_user' => $arr->id_user,
- 'ip' => $arr->ip,
- 'type' => $arr->type,
- 'description' => $arr->description,
- 'created_at' => $arr->created_at,
- 'updated_at' => $arr->updated_at,
- ];
- }
- if ($data) {
- Log::query()->insert($data);
- }
- }
- }
|