Skip to content

amsrafid/laravel-activitylog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Activity Log

Simple but very powerful activity log package for laravel framework.

Installation

To install this package, run this command to root directory of the project,

composer require amsrafid/laravel-activitylog

To publish config file of this package, run the following command,

php artisan vendor:publish --provider="Amsrafid\ActivityLog\ActivityLogServiceProvider"

Note: Publishing service provider is optional.

Get Started

Activity log has to be started after migrating log table. To make migration, hit the command bellow to the terminal,

php artisan migrate

Basic Use

Activity Log is very easy to use. A trait named ActivityLogHandler is to be added to any model to activate logging. On the other hand log can be enabled manually by initiating Logging class. Auto logging system can be added as like bellow,

namespace App;

use Illuminate\Database\Eloquent\Model;
use Amsrafid\ActivityLog\Traits\ActivityLogHandler;

class MyModel extends Model
{
    use ActivityLogHandler;

    // Optionals
    protected $log_name = "Log name";

    protected $description = "Log description";

    protected $ignore_fields = [
        // if any fields that being ignored to be logged
    ];

    protected $ignore_log = [
        // if any logging mode is to be ignored in (insert, update, delete, forceDelete)
    ];
}

Manual logging system is added bellow,

$myModel = MyModel::find(1);
$myModel->data = 'value';
$log = new Logging($myModel, 'update'); // model instance, mode -> [insert, update, delete, forceDelete]
$log->start();

OR

// Insertion operation
$myModel = new MyModel;
$myModel->data = 'value';
$myModel->save();

// Create new Activity Log
$log = new Logging(MyModel::class, 'insert');   // model name, mode -> [insert, update, delete, forceDelete]
$log->property([
    'new' => $myModel->toArray()
]);
$log->logName('Save my model');
$log->description('My model log has been created manually.');
$log->primaryId($myModel->id);
$log->start();

Here, Property can set by using setProperty(array new, array old) method. Where, 2 arguments can be performed to set new and old property of model. Here, default is an empty array for both.

Note: Clear configuration cache to active configuration file. Otherwise, log may not be created.

Barrier

Logging can be paused at any time and to be proceed by using paused and proceed static method respectively.

$myModel = new MyModel;
$myModel->data = 'value 1'; // Log created
$myModel->save();

Logging::paused();  // Logging become paused

$myModel->data = 'value 2'; // Log not created
$myModel->save();

Logging::proceed(); // Logging proceed again

$myModel->data = 'value 3'; // Log created
$myModel->save();

Note: Log barrier can be checked by isPaused static method.

Parsing of log

Log can be parsed by using \Amsrafid\ActivityLog\Models\ActivityLog\ActivityLog::class that extends with \Illuminate\Database\Eloquent\Model. So, ActivityLog::class acts same as a regular model. Here, properties property returns an array to better use.

$set = ActivityLog::latest()->first();

echo $set->properties['new']['description'];

Log cleaning up

Log can be deleted when a lot of activity has been recorded. To solve this problem, custom artisan command clear:log can help. It can operate when run the command bellow to the command window,

php artisan clear:log

OR

php artisan clear:log --day=7

OR

php artisan clear:log --date=2021-03-19

Here, option --date denotes the date before log will be deleted and --day to the number of day(s) before log will be cleared. Option day will not be applicable when date is given.

To operate cleaning automatically, a schedule can be created to console Kernel like bellow,

// ~/app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('clear:log')->daily();
}

Note: Data will be deleted the day(s) before the value of the key clean_log_before_days given into config file when --date or --day option is not given.

Authors

Initial development - A. M. Sadman Rafid

Security Vulnerabilities

If you discover a security vulnerability within Laravel Activity Log, please send an e-mail to A. M. Sadman Rafid via amsrafid@gmail.com. All security vulnerabilities will be promptly addressed.

License

The Laravel Activity Log is open-sourced software licensed under the MIT license.