org-SCAN / website

Site web du projet
6 stars 0 forks source link

[REFACTOR]Use Laravel logging system #509

Open lduf opened 2 days ago

lduf commented 2 days ago

What should be refactored?

We currently don't use the default Laravel logging system: very bad.

We should stop using ApiLog model which store in DB.

Use Laravel loggers to replace this :

<?php

namespace App\Models;

use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ApiLog extends Model
{
    use Uuids;
    use SoftDeletes;
    use HasFactory;
    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */

    public $incrementing = false;
    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';
    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    public static function getUser($push_id)
    {
        return self::find($push_id)->user_id;
    }

    public static function createFromRequest($request, $model = null)
    {
        $log = array();
        $log["user_id"] = $request->user()->id;
        $log["application_id"] = (!empty($request->header("Application-id")) ? $request->header("Application-id") : (!empty($request->validated()[0]["application_id"]) ? $request->validated()[0]["application_id"] : "website"));
        $log["api_type"] = $request->path();
        $log["http_method"] = $request->method();
        $log["model"] = $model;
        $log["ip"] = $request->ip();
        $log["crew_id"] = $request->user()->crew->id;

        return self::create($log);
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->withTrashed();
    }

    public function crew()
    {
        return $this->belongsTo(Crew::class)->withDefault();
    }

    public function getPushedDatas(){
        $base_name = "\App\Models\\";
        $pushed_datas = ($base_name . $this->model)::where("api_log", $this->id)->get();
        $res = array();
        foreach ($pushed_datas as $pushed_data) {
            $res[$pushed_data->id] = $pushed_data->bestDescriptiveValue;
        }

        return $res;
    }
}

@php use App\Models\User; @endphp
@section('title', __('api_logs/index.view_api_logs'))
<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('api_logs/index.api_logs') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="flex flex-col">
                <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
                    <div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
                        <div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
                            <table class="min-w-full divide-y divide-gray-200">
                                <caption class="sr-only">{{ __('api_logs/index.api_logs') }}</caption>
                                <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col"
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500
                                        uppercase tracking-wider">
                                        {{ __('api_logs/index.status') }}
                                    </th>
                                    <th scope="col"
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500
                                        uppercase tracking-wider">
                                        {{ __('api_logs/index.date') }}
                                    </th>
                                    <th scope="col"
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500
                                        uppercase tracking-wider">
                                        {{ __('api_logs/index.user') }}
                                    </th>
                                    <th scope="col"
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500
                                        uppercase tracking-wider">
                                        {{ __('api_logs/index.type') }}
                                    </th>
                                    <th scope="col"
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500
                                        uppercase tracking-wider">
                                        {{ __('api_logs/index.action') }}
                                    </th>
                                </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                @foreach($logs as $log)
                                    <tr>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            @if($log->response == "success")
                                                <em class="fas fa-check-circle  text-green-500"></em>
                                            @else
                                                <em class="fas fa-times-circle text-red-500"></em>
                                            @endif
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            {{$log->created_at}}
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            @can('view', $log->user, User::class)
                                                <a href="{{ route("user.show", $log->user->id) }}"
                                                   class="text-indigo-600 hover:text-blue-900">{{ $log->user->email }}
                                                </a>
                                            @endcan()
                                            @cannot('view', $log->user,User::class)
                                                {{ $log->user->email }}
                                            @endcannot

                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            {{$log->api_type}}
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            @can('view', $log)
                                                <a href="{{ route("api_logs.show", $log->id) }}"
                                                   class="text-indigo-600 hover:text-blue-900">{{ __('api_logs/index.view_detail') }}
                                                </a>
                                            @endcan
                                        </td>
                                    </tr>
                                @endforeach
                                <!-- More items... -->
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</x-app-layout>
``

**Additional information**
You can put all the information you want here.
github-actions[bot] commented 2 days ago

AI-Assisted Response:

Issue Summary: Refactoring to Use Built-In Laravel Logging System

Issue Category: Refactoring / Feature Request

Summary: The current project setup deviates from the conventional Laravel logging system by using a custom ApiLog model to store logs in the database. The issue requires refactoring the code to utilize Laravel's built-in logging mechanisms.

Details:

Potential Impact:

Suggestions for Issue Resolution:

  1. Identify all instances where ApiLog is used and determine how the logs are currently consumed.
  2. Refactor the code to utilize Laravel's default logging channels (such as files or other drivers defined in config/logging.php).
  3. Provide a migration strategy for any historical log data if necessary.
  4. Update all views and controllers that reference ApiLog to work with Laravel's logging system or remove them if no longer needed.
  5. Thoroughly test the application to ensure that all logging functionalities work as expected after the refactor.

These suggestions are a starting point and might not fully resolve the issue. Further investigation and a solid refactoring plan are needed.

Clarification Questions for @lduf

  1. Are there specific reasons why database storage was initially used for logging, such as audit requirements or user interface features?
  2. Is there a need to migrate existing logs from the database to the new logging system, and if so, what format and storage medium should be used?
  3. Are there any specific features or functionalities provided by the ApiLog model and associated views/controllers that need to be preserved in the new logging approach?
  4. What log levels (e.g., debug, info, warning, error) does the application currently use, and do they need to be mapped to equivalent levels in Laravel's built-in logging system?
  5. Can you confirm if the logging change should be backward compatible or if it's acceptable to start fresh with the new system
create-issue-branch[bot] commented 2 days ago

Branch feature/issue-509-_REFACTOR_Use_Laravel_logging_system created!