Package Data | |
---|---|
Maintainer Username: | rejtg21 |
Maintainer Contact: | freek@spatie.be (Freek Van der Herten) |
Package Create Date: | 2016-07-08 |
Package Last Update: | 2016-07-13 |
Home Page: | https://spatie.be/opensource/laravel |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:06:03 |
Package Statistics | |
---|---|
Total Downloads: | 50 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This package has been abandoned on 2016-07-28. Please use laravel-activitylog instead.
This Laravel 5 package provides a very easy to use solution to log the activities of the users of your Laravel 5 app. All the activities will be logged in a db-table. Optionally the activities can also be logged against the default Laravel Log Handler.
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
If you're using Laravel 4, take a look at version 0.3.0 of this package.
This package can be installed through Composer.
composer require rejtg21/activitylog
This service provider must be registered.
// config/app.php
'providers' => [
'...',
'Spatie\Activitylog\ActivitylogServiceProvider',
];
You'll also need to publish and run the migration in order to create the db-table.
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate
Activitylog also comes with a facade, which provides an easy way to call it.
// config/app.php
'aliases' => [
...
'Activity' => 'Spatie\Activitylog\ActivitylogFacade',
];
Optionally you can publish the config file of this package.
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
The configuration will be written to config/activitylog.php
. The options provided are self explanatory.
Logging some activity is very simple.
//at the top of your file you should import the facade.
use Activity;
...
/*
The log-function takes two parameters:
- $text: the activity you wish to log.
- $user: optional can be an user id or a user object.
if not proved the id of Auth::user() will be used
*/
Activity::log('Some activity that you wish to log');
The string you pass to function gets written in a db-table together with a timestamp, the ip address and the user agent of the user.
This package can log the events from your models. To do so your model must use the LogsActivity
-trait and implement LogsActivityInterface
.
use Spatie\Activitylog\LogsActivityInterface;
use Spatie\Activitylog\LogsActivity;
class Article implements LogsActivityInterface {
use LogsActivity;
...
The interface expects you to implement the getActivityDescriptionForEvent
-function.
Here's an example of a possible implementation.
/**
* Get the message that needs to be logged for the given event name.
*
* @param string $eventName
* @return string
*/
public function getActivityDescriptionForEvent($eventName)
{
if ($eventName == 'created')
{
return 'Article "' . $this->name . '" was created';
}
if ($eventName == 'updated')
{
return 'Article "' . $this->name . '" was updated';
}
if ($eventName == 'deleted')
{
return 'Article "' . $this->name . '" was deleted';
}
return '';
}
The result of this function will be logged, unless the result is an empty string.
If you want to disable logging under certain conditions,
such as for a specific user, create a class in your application
namespace that implements the Spatie\Activitylog\Handlers\BeforeHandlerInterface
.
This interface defines an shouldLog($data)
method in which you can code any custom logic to determine
whether logging should be ignored or not. You must return true
the call should be logged.
To en the namespaced class nameto the beforeHandler
field in the configuration file:
'beforeHandler' => '\App\Handlers\BeforeHandler',
For example, this callback class could look like this to disable logging a user with id of 1:
<?php
namespace App\Handlers;
use Spatie\Activitylog\Handlers\BeforeHandlerInterface;
class BeforeHandler implements BeforeHandlerInterface
{
public function shouldLog($data)
{
if ($data->user_id == 1) return false;
return true;
}
}
If you want to do something after the activity has been saved like for example
saving also in another table, create a class that implements
Spatie\Activitylog\Handlers\AfterHandlerInterface
.
this interface defines a function shouldLogAfter($data)
which you can create a
your own function that will be triggered after saving the activity. Be sure to
return the data received from parameter.
Be sure to specify the namespaced class in the configuration file
'afterHandler' => 'App\Http\Controllers\ActivityController',
For example,
<?php namespace App\Http\Controllers
use Spatie\Activitylog\Handlers\AfterHandlerInterface;
class ActivityController extends Controller implements AfterHandlerInterface
{
public function shouldLogAfter($data)
{
// logic here
return $data;
}
}
You can also add data to be passed in function specified in the interface in its second parameter it accepts array of data to carried to the function as object.
For example using afterHandler specified above,
using first the Activity::log();
$data = [
'partner_id' => 1
];
Activity::log('successfully logged', $data);
it will be catch like this,
public function shouldLogAfter($data)
{
$data->partner_id;
// other logic here
return $data;
}
And if you want to retreive the activity, that have been inserted, in the
shouldLogAfter
function, you can by using $data->activity
For example,
public function shouldLogAfter($data)
{
$data->activity->id;
$data->activity->ip_address;
// model explanation below
$data->activity->partner()->attach($data->partner_id);
}
Note: the model used in shouldLogAfter()
function is the default or
specified model in configuration. More info about this below.
if using Activity::log() just assign it in the variable
$activity = Activity::log('successfully logged');
/**
* you can already access the object
*/
$activty->text;
$activity->id;
Using the trait in model if you want to add data to be passed in the function. It goes like this.
// declare first the variable activityData
public $activtyData = [];
...
public function getActivityDescriptionForEvent($eventName)
{
// insert some data
$this->activityData = [
'partner_id' => $this->id,
];
if($eventName == 'created')
return 'successfully created';
}
If you want to specify spatie to use your own model instead of default model you can specify it in configuration file
'modelPath' => 'App\Models\Activity'
By specifying this you informed spatie that it will use your model throughout the library. But if you only want to extend the model of spatie and add only other functions you still can, just extend the spatie model
<?php namespace App\Models
use Spatie\Activitylog\Models\Activity as Spatie;
class Activity extends Spatie
{
public function partner()
{
return $this->belongsToMany('App\Models\User', 'user_activities',
'activity_id', 'user_id');
}
//add other functions here ....
}
All events will be logged in the activity_log
-table. This package provides an Eloquent model to work with the table. You can use all the normal Eloquent methods that you know and love. Here's how you can get the last 100 activities together with the associated users.
use Spatie\Activitylog\Models\Activity;
$latestActivities = Activity::with('user')->latest()->limit(100)->get();
Over time your log will grow. To clean up the database table you can run this command:
Activity::cleanLog();
By default records older than 2 months will be deleted. The number of months can be modified in the config-file of the package.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
The MIT License (MIT). Please see License File for more information.