2017 © Pedro Peláez
 

library laravel-activity-monitor

The activity logged to monitor your website

image

uatthaphon/laravel-activity-monitor

The activity logged to monitor your website

  • Wednesday, February 21, 2018
  • by uatthaphon
  • Repository
  • 0 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

laravel-activity-mornitor

Activity Monitor Log is an activity logger for monitoring user activity and eloquent models events activity., (*1)

Note: This package use laravel 5.5+ it require php 7.0+, (*2)

Setup

Add package dependency to your project, (*3)

composer require uatthaphon/laravel-activity-monitor

As the package build for laravel 5.5+, I use Auto-Discovery so we don't need to add service provider in config\app.php anymore, (*4)

Run publishing to get the database migration for table activity_monitors, (*5)

php artisan vendor:publish --tag=migrations

After published, we can create table by running the migrations, (*6)

php artisan migrate

Usage

Logger

This package have 2 aliases AMLog and AMView for us to easily use to save the log and view the logs., (*7)

We don't need to add those 2 aliases to config/app.php neither. It already added for us by Auto-Discovery., (*8)

Example, log the user updated their post., (*9)

use AMLog;

$post = Post::where('user_id', $id)->firstOrFail();
$post->body = 'update body content';
$post->save();

AMLog::logName('custom log name')               // Declare log name
    ->description('user updated post content')  // Log description
    ->happenTo($post)                           // Model of the event happen to
    ->actBy(\Auth::user())                      // Model that cause this event
    ->meta(['key'=>'value'])                    // Additional pieces of information
    ->save();                                   // Let's Save the log

AMlog also prepared some of the log name for us to easily use => debug, error, fatal, info, warning, (*10)

use AMLog;
...
AMLog::debug('some debug description')->save();

AMLog::error('some error description')->save();

AMLog::fatal('some fatal description')->save();

AMLog::info('some info description')->save();

AMLog::warning('some warning description')->save();

That's it :notes:, (*11)

Eloquent Models Events Log

For you to easy log your eloquent model activities when created, updated, deleted., (*12)

After you setting up the package then add ModelEventActivity Trait to your model., (*13)

namespace App\Models;

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;
    ...
}

This feature will record only changes in your application by setting protected static $loggable to tell the logger which attributes should be logs., (*14)

Note: It will not log attribute that use database default value... Except you add value to the attribute by your self, (*15)

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $loggable = ['title', 'description']
}

If title record changed, It will only log title field in the table activity_monitors, (*16)

{"title": "has some change"}

We can cutomize which eloquent event should be log by protected static $eventsToLog., (*17)

In the example below only created event for this model will be logged, (*18)

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $eventsToLog = ['created']
}

We can add our meta data to each event by add this to yout model, (*19)

...
use Uatthaphon\ActivityMonitor\Traits\ModelEventActivity;

class ToBelog extends Model
{
    use ModelEventActivity;

    protected static $createdEventMeta = ['create key' => 'create value'];

    protected static $updatedEventMeta = ['update key' => 'update value'];

    protected static $deletedEventMeta = ['deletd key' => 'delete value'];

    ...
}

View Logs

We can use AMView to get our logs it will return as ActivityMonitor, (*20)

See this example below, (*21)

use AMView;

...

// Get all
AMView::all();                                    // get all the logs
AMView::get();                                    // also act the same as all()

// With conditions
AMView::logName('your_log_name')                  // get by log name
    ->limit(5)                                    // limit resutls
    ->sortBy('desc')                              // sort By desc or asc
    ->get();

// Get from multiple log names
AMView::logName('info', 'updated')->get();
AMView::logName(['info', 'updated'])->get();


// Get all specific lastest post log From current user
$user = \Auth::user();
$post = $user->post()->last($user);
AMView::happenTo($post)->ActBy($user)->get();

// Or call from providings log name function
AMView::debug()->all();

AMView::error()->all();

AMView::fatal()->all();

AMView::info()->all();

AMView::warning()->all();

...

Try and see. It will return collection of ActivityMonitor model, (*22)

use AMView;

...

$am = AMView::info()->all()->last();

$am->log_name;                          // Get log name
$am->description;                       // Get description
$am->agent;                             // Get user browser agent
$am->ip;                                // Get user ip address

$traces = $am->traces;                  // Get traces

foreach ($traces as $key => $value) {
    // do something
}

$meta = $am->meta;                      // Get you custom meta data

foreach ($meta as $key => $value) {
    // do something
}

...

View Log In Specific Model

We can add ActivityMonitor to our model, (*23)

...
use Uatthaphon\ActivityMonitor\Traits\ActivityMonitorRelations;

class User extends Authenticatable
{
  use ActivityMonitorRelations;

  ...
}

Now we can use activity() polymorphic relations, (*24)

// Get all activity records of the current user
\Auth::user()->activity()->get();

// Retrived with more specific
// By tell with model record user was interact with
$user = \Auth::user();
$user->activity()->happenTo($user->posts()-last())->get();

// Use the providing log name function
$user->activity()->info()->get();

// Use the providing log name with specific fom model togetter
$user->activity()
    ->info()
    ->happenTo($user->posts()-last())
    ->get();

The Versions

21/02 2018

dev-master

9999999-dev

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

21/02 2018

0.1.8

0.1.8.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.7

0.1.7.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.6

0.1.6.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.4

0.1.4.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.3

0.1.3.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.0

0.1.0.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty

16/02 2018

0.1.1

0.1.1.0

The activity logged to monitor your website

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log monitor acitivty