Code Monkey home page Code Monkey logo

activitylog's Introduction

Log the activity of your users

Latest Version Software License Build Status SensioLabsInsight Total Downloads

EOL-warning

This package has been abandoned on 2016-06-28. Please use laravel-activitylog instead.

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.

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

Note:

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

Installation

This package can be installed through Composer.

composer require spatie/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.

Usage

Manual logging

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.

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.

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.

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.

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.

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($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.

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:

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.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

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.

License

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

activitylog's People

Contributors

freekmurze avatar gauravmak avatar kerwitz avatar lowerends avatar matthiasdewinter avatar mul14 avatar niban avatar ozgurkaragoz avatar sebastiandedeyne avatar themsaid avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.