PHPackages                             ge-tracker/basic-activity-log - 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. ge-tracker/basic-activity-log

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

ge-tracker/basic-activity-log
=============================

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

v5.0.1(3y ago)03.0kMITPHPPHP ^8.0

Since May 6Pushed 3y agoCompare

[ Source](https://github.com/ge-tracker/basic-activity-log)[ Packagist](https://packagist.org/packages/ge-tracker/basic-activity-log)[ Docs](https://github.com/ge-tracker/basic-activity-log)[ RSS](/packages/ge-tracker-basic-activity-log/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (8)Versions (29)Used By (0)

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

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

[![Latest Version](https://camo.githubusercontent.com/cf5701ea3d9c868d70407670ee778a291b015409d1c8d78c442449fec1a8ef18/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f67652d747261636b65722f62617369632d61637469766974792d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://github.com/ge-tracker/basic-activity-log/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/704a1fdb5184acdaecd3248188072d07ad3ae10a2352110122b7a891b7db5115/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67652d747261636b65722f62617369632d61637469766974792d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ge-tracker/basic-activity-log)

Description
-----------

[](#description)

This Laravel package provides a very easy to use solution to log the activities of the users of your Laravel 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, a webdesign agency in Antwerp, Belgium. It was forked on 2020-04-01 by GE Tracker and renamed, as we make use of this package in our production application.

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

[](#installation)

This package can be installed through Composer.

```
composer require ge-tracker/basic-activity-log
```

This service provider will automatically be registered.

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

```
php artisan vendor:publish --provider="GeTracker\BasicActivityLog\BasicActivityLogServiceProvider" --tag="migrations"
php artisan migrate
```

Optionally you can publish the config file of this package.

```
php artisan vendor:publish --provider="GeTracker\BasicActivityLog\BasicActivityLogServiceProvider" --tag="config"
```

The configuration will be written to `config/basic-basic-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 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

[](#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 GeTracker\BasicActivityLog\Contracts\LogsActivity as LogsActivityContract;
use GeTracker\BasicActivityLog\LogsActivity;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements LogsActivityContract
{
   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.

[](#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 `GeTracker\BasicActivityLog\Contracts\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 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 id of 1:

```
