2017 © Pedro Peláez
 

library activitylog

A very simple activity logger to monitor the users of your website or application

image

spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  • Friday, July 29, 2016
  • by Spatie
  • Repository
  • 28 Watchers
  • 430 Stars
  • 142,373 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 63 Forks
  • 0 Open issues
  • 20 Versions
  • 7 % Grown

The README.md

Log the activity of your users

Latest Version Software License Build Status SensioLabsInsight Total Downloads, (*1)

EOL-warning

This package has been abandoned on 2016-06-28. Please use laravel-activitylog instead., (*2)

Description

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., (*3)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*4)

Note:

If you're using Laravel 4, take a look at version 0.3.0 of this package., (*5)

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using., (*6)

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*7)

The best postcards will get published on the open source page on our website., (*8)

Installation

This package can be installed through Composer., (*9)

composer require spatie/activitylog

This service provider must be registered., (*10)

// config/app.php

'providers' => [
    '...',
    'Spatie\Activitylog\ActivitylogServiceProvider',
];

You'll also need to publish and run the migration in order to create the db-table., (*11)

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., (*12)

// config/app.php

'aliases' => [
    ...
    'Activity' => 'Spatie\Activitylog\ActivitylogFacade',
];

Optionally you can publish the config file of this package., (*13)

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., (*14)

Usage

Manual logging

Logging some activity is very simple., (*15)

//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., (*16)

Log model events

This package can log the events from your models. To do so your model must use the LogsActivity-trait and implement LogsActivityInterface., (*17)

use Spatie\Activitylog\LogsActivityInterface;
use Spatie\Activitylog\LogsActivity;

class Article implements LogsActivityInterface {

   use LogsActivity;
...

The interface expects you to implement the getActivityDescriptionForEvent-function., (*18)

Here's an example of a possible implementation., (*19)

/**
 * 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., (*20)

Using a before handler.

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., (*21)

This interface defines an shouldLog() 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., (*22)

To en the namespaced class nameto the beforeHandler field in the configuration file:, (*23)

'beforeHandler' => '\App\Handlers\BeforeHandler',

For example, this callback class could look like this to disable logging a user with id of 1:, (*24)

<?php

namespace App\Handlers;

use Spatie\Activitylog\Handlers\BeforeHandlerInterface;

class BeforeHandler implements BeforeHandlerInterface
{
    public function shouldLog($text, $userId)
    {
        if ($userId == 1) return false;

        return true;
    }
}

Retrieving logged entries

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., (*25)

use Spatie\Activitylog\Models\Activity;

$latestActivities = Activity::with('user')->latest()->limit(100)->get();

Cleaning up the log

Over time your log will grow. To clean up the database table you can run this command:, (*26)

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., (*27)

Contributing

Please see CONTRIBUTING for details., (*28)

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*29)

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*30)

License

The MIT License (MIT). Please see License File for more information., (*31)

The Versions

29/07 2016

dev-master

9999999-dev https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

29/06 2016

2.4.3

2.4.3.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

23/03 2016

2.4.2

2.4.2.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

16/02 2016

2.4.1

2.4.1.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

16/02 2016

2.4.0

2.4.0.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

25/12 2015

2.3.0

2.3.0.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

21/12 2015

2.2.1

2.2.1.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

15/10 2015

2.2.0

2.2.0.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

09/07 2015

2.1.3

2.1.3.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

22/05 2015

2.1.2

2.1.2.0 https://github.com/spatie/activitylog

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

21/05 2015

2.1.1

2.1.1.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

21/05 2015

2.1.0

2.1.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

01/04 2015

2.0.0

2.0.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel log user activity

05/02 2015

1.0.1

1.0.1.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

05/02 2015

1.0.0

1.0.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

26/01 2015

0.3.0

0.3.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

02/06 2014

0.2.1

0.2.1.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

06/05 2014

0.2.0

0.2.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

06/05 2014

0.1.0

0.1.0.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity

06/05 2014

0.1.1

0.1.1.0

A very simple activity logger to monitor the users of your website or application

  Sources   Download

MIT

The Requires

 

laravel log user activity