# CakePHP 4 ActivityLogger plugin

> Activity logging is one of the very useful features in web applications since it can provide a track of activities in the web content. CakePHP Activity Logger Plugin enables the developer or administrator to log the activities and changes to the web content.

- URL: https://codethepixel.com/codethepixel/public/activitylogger-plugin-for-cakephp-4-x
- Author: Asyraf Wahi Anuar (https://codethepixel.com/codethepixel/public/author/asyraf-wahi-anuar)
- Published: 2021-11-30
- Category: CakePHP
- Tested with: CakePHP 4.x
- Tags: CakePHP, Plugin, Log, Tracker
- Reading time: 3 min

Activity logging is one of the very useful features in web applications since it can provide a track of activities in the web content. CakePHP Activity Logger Plugin enables the developer or administrator to log the activities and changes to the web content. This tutorial will show how to integrate the CakePHP Activity Logger Plugin into your web application.

Repository: [https://github.com/elstc/cakephp-activity-logger](https://github.com/elstc/cakephp-activity-logger)

## Installation

Currently, when this tutorial is written, the v2.0.0-rc5 is the latest compatible version with CakePHP 4. Use the following command to download:

```bash
$ composer require elstc/cakephp-activity-logger:v2.0.0-rc5
```

or if a stable version has been released for CakePHP 4, use the following command:

```bash
$ composer require elstc/cakephp-activity-logger
```

## Load plugin

Load the plugin in .../src/Application.php:

`src/Application.php`

```php
$this->addPlugin('Elastic/ActivityLogger');
```

## Create activity\_logs Table

run migration command in the console:

```bash
$ bin/cake migrations migrate -p Elastic/ActivityLogger
```

## Log Behaviour

Add the following code to your table eg in .../src/Model/Table/ArticlesTable.php

`src/Model/Table/ArticlesTable.php`

```php
$this->addBehavior('Elastic/ActivityLogger.Logger', [
    'scope' => [
        //'Users',
        //'Authors',
    ],
    'systemScope' => false,
]);
```

At this point, it will log create, update and delete action. If you need to set the log message, add the following code before save method.

```php
$this->Articles->setLogMessage('Transfer to Archived', true);
```

That's all. Happy coding :)
