PHPackages                             codemyviews/activitylog - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. codemyviews/activitylog

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

codemyviews/activitylog
=======================

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

v1.0.1(10y ago)03.7k1MITPHPPHP &gt;=5.4.0

Since Apr 6Pushed 10y ago3 watchersCompare

[ Source](https://github.com/codemyviews/activitylog)[ Packagist](https://packagist.org/packages/codemyviews/activitylog)[ Docs](https://github.com/codemyviews/activitylog)[ RSS](/packages/codemyviews-activitylog/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Log the activity of your users
==============================

[](#log-the-activity-of-your-users)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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.

This package was originally created by Spatie. Spatie is a web design agency in Antwerp, Belgium. You'll find an overview of all their open source projects [on their website](https://spatie.be/opensource).

Later we in CodeMyViews decided to modify Spatie version to better fit our needs. You can read about us on [our site](https://CodeMyViews.com/).

Installation
------------

[](#installation)

This package can be installed through Composer.

```
composer require codemyviews/activitylog
```

This service provider must be registered.

```
// config/app.php

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

You'll also need to publish and run the migration in order to create the db-table.

```
php artisan vendor:publish --provider="CodeMyViews\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' => 'CodeMyViews\Activitylog\ActivitylogFacade',
];
```

Optionally you can publish the config file of this package.

```
php artisan vendor:publish --provider="CodeMyViews\Activitylog\ActivitylogServiceProvider" --tag="config"

```

The configuration will be written to `config/activitylog.php`. The options provided are self explanatory.

Usage
-----

[](#usage)

### Manual logging

[](#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 three parameters:
      - $text: the activity you wish to log.
      - $user:  optional can be a user id or a user object.
                if not proved the id of Auth::user() will be used
      - $model: optional can be an instance of Illuminate\Database\Eloquent\Model
                if not provided, none will be used
                if provided, activity will be referencing this model
*/
Activity::log('Some activity that you wish to log');
Activity::log('User has updated a post', $user, $post);
```

The string you pass to function gets written in a db-table together with a timestamp, the IP address, the user agent of the user, and reference to the model.

### Log model events

[](#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. Logged activities will be referencing the model, which has created them.

### Using a before handler.

[](#using-a-before-handler)

If you want to disable logging under certain conditions, such as for a particular user, create a class in your application namespace that implements the `CodeMyViews\Activitylog\Handlers\BeforeHandlerInterface`.

This interface defines a `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 name to 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 the id of 1:

```
