PHPackages                             aliirfaan/laravel-simple-audit-log - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. aliirfaan/laravel-simple-audit-log

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

aliirfaan/laravel-simple-audit-log
==================================

This package creates a database table to save audit logs and exposes events to log user actions.

10.0.0(1y ago)02041MITPHP

Since Jan 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/aliirfaan/laravel-simple-audit-log)[ Packagist](https://packagist.org/packages/aliirfaan/laravel-simple-audit-log)[ RSS](/packages/aliirfaan-laravel-simple-audit-log/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)DependenciesVersions (15)Used By (1)

Laravel Simple Audit Log
========================

[](#laravel-simple-audit-log)

Many systems need to log user actions for auditing purposes. This package creates a database table with sensible fields for logging actions.

Features
--------

[](#features)

- Table structure to keep audit logs
- Event driven
- Configurable connection if using a different database for recording logs
- Configurable model
- Audit log service
- log request ip, app environment

Table fields
------------

[](#table-fields)

This is only a dump to explain fields. Table will be created via Laravel migration file.

```
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();
});
```

Events
------

[](#events)

You can dispatch these events to record logs. You can also listen to these events if you want additional processing.

- AuditLogged

Requirements
------------

[](#requirements)

- [Composer](https://getcomposer.org/)
- [Laravel](http://laravel.com/)
- [MySQL 4.x +](https://www.mysql.com/) VARBINARY data type is available as from 4.x

Installation
------------

[](#installation)

You can install this package on an existing Laravel project with using composer:

```
 $ composer require aliirfaan/laravel-simple-audit-log
```

Register the ServiceProvider by editing **config/app.php** file and adding to providers array:

```
  aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider::class,
```

Note: use the following for Laravel &lt;5.1 versions:

```
 'aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider',
```

Publish files with:

```
 $ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider"
```

or by using only `php artisan vendor:publish` and select the `aliirfaan\LaravelSimpleAuditLog\SimpleAuditLogProvider` from the outputted list.

Apply the migrations:

```
 $ php artisan migrate
```

Configuration
-------------

[](#configuration)

This package publishes an `simple-audit-log.php` file inside your applications's `config` folder which contains the settings for this package. Most of the variables are bound to environment variables, but you are free to directly edit this file, or add the configuration keys to the `.env` file.

audit\_log\_db\_connection | String
The database connection to use. Defaults to environment variable 'DB\_CONNECTION'.

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

audit\_log\_model | String The model you want to use. The model must implement aliirfaan\\LaravelSimpleAuditLog\\Contracts\\SimpleAuditLog

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

should\_prune | Boolean Whether to prune records

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

prune\_days | Numeric to delete records older than prune\_days

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

Usage
-----

[](#usage)

```
