Laravel Simple Access Log

Overview

Laravel Simple Access Log

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

Features

  • Table structure to keep access logs
  • Events for common access operations like login success, login failure, logout
  • Configurable connection if using a different database for recording logs
  • Use custom model

Table fields

Migration schema to explain available fields.

Schema::connection(config('simple-access-log.access_log_db_connection'))->create('lsac_access_logs', function (Blueprint $table) {
    $table->id();
    $table->dateTime('ac_date_time_local', $precision = 0)->index('ac_date_time_local_index')->comment('Timestamp in local timezone.');
    $table->dateTime('ac_date_time_utc', $precision = 0)->nullable()->index('ac_date_time_utc_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('ac_actor_type', 255)->nullable()->index('ac_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('ac_actor_username', 255)->nullable()->index('ac_actor_username_index')->comment('Username in application.');
    $table->string('ac_actor_group', 255)->nullable()->index('ac_actor_group_index')->comment('User role/group in application.');
    $table->string('ac_device_id', 255)->nullable()->index('ac_device_id_index')->comment('Device identifier.');
    $table->string('ac_event_name', 255)->index('ac_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->ipAddress('ac_ip_addr')->nullable()->index('ac_ip_addr_index');
    $table->string('ac_server', 255)->nullable()->index('ac_server_index')->comment('Server ids or names, server location. Example: uat, production, testing, 192.168.2.10');
    $table->string('ac_version', 255)->nullable()->index('ac_version_index')->comment('Version of the code/release that is sending the events.');
    $table->timestamps();
});

Events

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

  • LoginSucceeded
  • LoginFailed
  • LoggedOut

Requirements

Installation

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

 $ composer require aliirfaan/laravel-simple-access-log

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

  aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider::class,

Note: use the following for Laravel <5.1 versions:

 'aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider',

Publish files with:

 $ php artisan vendor:publish --provider="aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider"

or by using only php artisan vendor:publish and select the aliirfaan\LaravelSimpleAccessLog\SimpleAccessLogProvider from the outputted list.

Apply the migrations:

 $ php artisan migrate

Configuration

This package publishes an simple-access-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.

access_log_db_connection | String
The database connection to use. Defaults to environment variable 'DB_CONNECTION'.

'access_log_db_connection' => env('ACCESS_LOG_DB_CONNECTION', env('DB_CONNECTION'))

access_log_model | String The model you want to use. The model must implement aliirfaan\LaravelSimpleAccessLog\Contracts\SimpleAccessLog

'access_log_model' => aliirfaan\LaravelSimpleAccessLog\Models\SimpleAccessLog::class,

Usage



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use aliirfaan\LaravelSimpleAccessLog\Events\LoginSucceeded; // event you want to dispatch

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

            // log access after operation
            $eventData = [
                'ac_date_time_local' => date('Y-m-d H:i:s'),
                'ac_date_time_utc' => date('Y-m-d H:i:s'),
                'ac_actor_id' => 5,
                'ac_actor_type' => 'Model/Customer',
                'ac_actor_global_uid'=> 5,
                'ac_actor_username' => 'actor username',
                'ac_actor_group' => 'actor group',
                'ac_device_id' => 5,
                'ac_event_name' => 'user.login.success',
                'ac_server' => 'uat',
                'ac_version'=> 'version',
                'ac_ip_addr'=> $request->ip()
            ];

            // dispatch event
            LoginSucceeded::dispatch($eventData);

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

Custom model

If you have have additional requirements for our access logs, you can add columns using migation and use a custom model to use your new columns.



namespace App\Models\AccessLog;

use Illuminate\Database\Eloquent\Model;
use aliirfaan\LaravelSimpleAccessLog\Contracts\SimpleAccessLog as SimpleAccessLogContract;
use aliirfaan\LaravelSimpleAccessLog\Models\SimpleAccessLog;

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

Specify custom model to configuration file

'access_log_model' => App\Models\AccessLog\AccessLog::class,

License

The MIT License (MIT)

Copyright (c) 2020

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
Log executed Laravel SQL queries and their line number and more

A lightweight laravel package for logging executed SQL queries, line number and more

Laravel Email Audit Log

This service provider will monitor all emails that has been sent out of your system. Sent emails will be stored in email_audit_log table

Laravel telegram log is a package that can catch your logs all quite simply

Laravel Telegram log Laravel telegram log is a package that can catch your logs all quite simply. Requirments This package is tested with Laravel v8 i

OPcodes's Log Viewer is a perfect companion for your Laravel app
OPcodes's Log Viewer is a perfect companion for your Laravel app

Log Viewer Easy-to-use, fast, and beautiful Features | Installation | Configuration | Authorization | Troubleshooting | Credits OPcodes's Log Viewer i

Log requests and group together for aggregated statistics of route usage
Log requests and group together for aggregated statistics of route usage

Log Laravel route usage statistics Log Laravel requests and responses for statistical purposes and optionally aggregate by hours/days/months for minim

The Laravel Boilerplate Project - https://laravel-boilerplate.com - For Slack access, visit:

Laravel Boilerplate (Current: Laravel 8.*) (Demo) Demo Credentials Admin: [email protected] Password: secret User: [email protected] Password: secret Offici

Provides access to Pexels API for Laravel projects

Laravel Pexels Provides access to Pexels API for Laravel projects Table of contents Installation Using Installation To get the latest version of Larav

A package to access messari apis for laravel

Messari API - Laravel Laravel wrapper for messari.io API Full API documentation could be found on messari.io Installation PHP 7.2+ and Composer are re

A laravel package to access data from the Strava API.

Laravel Strava Package A laravel package to access data from the Strava API. Compatible with Laravel 5.0 and above. Table of Contents Strava Access Cr

Releases(3.0.0)
Owner
Irfaan Nujjoo
"When not coding", I’m most likely reading blogs about the impact of technology on communities, watching videos about design and architecture...
Irfaan Nujjoo
Laravel Authentication Log is a package Log user authentication details and send new device notifications.

Laravel Authentication Log is a package which tracks your user's authentication information such as login/logout time, IP, Browser, Location, etc. as well as sends out notifications via mail, slack, or sms for new devices and failed logins.

Anthony Rappa 540 Jan 5, 2023
Access laravel log through Filament admin panel

Access laravel log through Filament admin panel Features Syntax highlighting Quickly jump between start and end of the file Refresh log contents Clear

Guilherme Saade 20 Nov 22, 2022
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php >= 7.4 Laravel version >= 6.0 Installation composer

Sagar 6 Aug 25, 2022
A simple Laravel event log package for easy model based logging.

Karacraft Logman A simple Model Event Logging Package Usage Installation composer require karacraft/logman Migrate php artisan migrate Publish php a

Karacraft 0 Dec 28, 2021
This package provides a Logs page that allows you to view your Laravel log files in a simple UI

A simplistics log viewer for your Filament apps. This package provides a Logs page that allows you to view your Laravel log files in a simple UI. Inst

Ryan Chandler 9 Sep 17, 2022
This project uses dflydev/dot-access-data to provide simple output filtering for cli applications.

FilterViaDotAccessData This project uses dflydev/dot-access-data to provide simple output filtering for applications built with annotated-command / Ro

Consolidation 44 Jul 19, 2022
Log activity inside your Laravel app

Log activity inside your Laravel app The spatie/laravel-activitylog package provides easy to use functions to log the activities of the users of your

Spatie 4.6k Jan 7, 2023
Record the change log from models in Laravel

This package will help you understand changes in your Eloquent models, by providing information about possible discrepancies and anomalies that could

Owen IT Services 2.5k Dec 30, 2022
This Package helps you in laravel application to log all desired activity for each request from request entry point to generate response at a single snapshot.

Laravel Scenario Logger This Package helps you in laravel application to log all desired activity for each request from request entry point to generat

Mehrdad Mahdian 6 Sep 27, 2021
Log user authentication actions in Laravel.

Laravel Auth Log The laravel-auth-log package will log all the default Laravel authentication events (Login, Attempting, Lockout, etc.) to your databa

Label84 29 Dec 8, 2022