PHP code example of aliirfaan / laravel-simple-audit-log

1. Go to this page and download the library: Download aliirfaan/laravel-simple-audit-log library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aliirfaan / laravel-simple-audit-log example snippets


Schema::connection(config('simple-audit-log.audit_log_db_connection'))->create('lsal_audit_logs', function (Blueprint $table) {
    $table->id();
    $table->dateTime('al_date_time', $precision = 0)->index('al_date_time_index');
    $table->string('al_actor_id')->nullable()->index('al_actor_id_index')->comment('User id in application. Can be null in cases where an action is performed programmatically.');
    $table->string('al_actor_type', 255)->nullable()->index('al_actor_type_index')->comment('Actor type in application. Useful if you are logging multiple types of users. Example: admin, user, guest');
    $table->string('al_actor_global_uid')->nullable()->index('al_actor_global_uid_index')->comment('User id if using a single sign on facility.');
    $table->string('al_actor_username', 255)->nullable()->index('al_actor_username_index')->comment('Username in application.');
    $table->string('al_actor_group', 255)->nullable()->index('al_actor_group_index')->comment('User role/group in application.');
    $table->string('al_device_id', 255)->nullable()->index('al_device_id_index')->comment('Device identifier.');
    $table->string('al_target_name', 255)->nullable()->index('al_target_name_index')->comment('The object or underlying resource that is being accessed. Example: user.');
    $table->string('al_target_id')->nullable()->index('al_target_id_index')->comment('The ID of the resource that is being accessed.');
    $table->string('al_action_type', 255)->nullable()->index('al_action_type_index')->comment('CRUD: Read, write, update, delete');
    $table->string('al_event_name', 255)->nullable()->index('al_event_name_index')->comment('Common name for the event that can be used to filter down to similar events. Example: user.login.success, user.login.failure, user.logout');
    $table->string('al_correlation_id', 255)->nullable()->index('al_correlation_id_index')->comment('Correlation id for easy traceability and joining with other tables.');
    $table->string('al_parent_correlation_id', 255)->nullable()->index('al_parent_correlation_id_index')->comment('Correlation id for easy traceability and joining with other tables.');
    $table->tinyInteger('al_is_success')->nullable()->default(0)->index('al_is_success_index');
    $table->text('al_meta')->nullable();
    $table->text('al_message')->nullable();
    $table->text('al_previous_value')->nullable();
    $table->text('al_new_value')->nullable();
    $table->text('al_request')->nullable()->comment('Request information.');
    $table->text('al_response')->nullable()->comment('Response information.');
    $table->ipAddress('al_ip_addr')->nullable()->index('al_ip_addr_index');
    $table->string('al_server', 255)->nullable()->index('al_server_index')->comment('Server ids or names, server location. Example: uat, production, testing, 192.168.2.10');
    $table->string('al_version', 255)->nullable()->index('al_version_index')->comment('Version of the code/release that is sending the events.');
    $table->string('al_log_level', 10)->nullable()->index('al_log_level_index')->comment('Log level.');
    $table->string('al_code', 50)->nullable()->index('al_code_index')->comment('Error code.');
    $table->timestamps();
});

  aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider::class,

 'aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider',

'audit_log_db_connection' => env('AUDIT_LOG_DB_CONNECTION', env('DB_CONNECTION'))

'audit_log_model' => aliirfaan\LaravelSimpleAuditLog\Models\SimpleAuditLog::class,

'should_prune' => env('AUDIT_LOG_SHOULD_PRUNE', false),

'prune_days' => env('AUDIT_LOG_PRUNE_DAYS', 30),



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use aliirfaan\LaravelSimpleAuditLog\Events\AuditLogged; // event you want to dispatch
use aliirfaan\LaravelSimpleAuditLog\Services\AuditLogService;

class TestController extends Controller
{
    public function test(Request $request, AuditLogService $auditLogService)
    {
        try {

            // you can use service to get preliminary audit data
            $correlationToken = 'a-token';
            $actor = null;
            $auditData = $auditLogService->generatePreliminaryAuditData($request, $correlationToken, $actor);

            // log access after operation
            $eventData = [
                'al_is_success'=> true,
                'al_code' => 'a-code'
            ];
            $auditData = array_merge($auditData, $eventData);

            // dispatch event
            AuditLogged::dispatch($auditData);

        } catch (\Exception $e) {
            report($e);
        }
    }
}



namespace App\Models\AuditLog;

use Illuminate\Database\Eloquent\Model;
use aliirfaan\LaravelSimpleAuditLog\Contracts\SimpleAuditLog as SimpleAuditLogContract;
use aliirfaan\LaravelSimpleAuditLog\Models\SimpleAuditLog;

// custom class that extends base model and implements contract
class AuditLog extends SimpleAuditLog implements SimpleAuditLogContract
{
    public function __construct(array $attributes = [])
    {
        // add your additional columns to the fillable property
        $this->mergeFillable(['al_custom_field_1']);
        
        parent::__construct($attributes);
    }
}

'audit_log_model' => App\Models\AuditLog\AuditLog::class,
bash
 $ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider"
bash
 $ php artisan migrate