Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 ActivityLogger plugin

Asyraf Wahi Anuar 3 min read

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 may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

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

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:

Terminal
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:

Terminal
composer require elstc/cakephp-activity-logger

Load plugin

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

src/Application.phpPHP
$this->addPlugin('Elastic/ActivityLogger');

Create activity_logs Table

run migration command in the console:

Terminal
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.phpPHP
$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 :)

Did this tutorial help?

270 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

CakePHP 4 Audit Log Plugin

An audit trail (audit logs) is a chronological log meant to capture any changes made to the electronic contents. The logs are used to be evidence for any changes that affected the contents.

3 min read

CakePHP

CakePHP 4 Soft Delete

A soft delete marks a record as no longer active or valid without deleting it from the database. Soft deletion is a widely used pattern applied for business applications.

5 min read

CakePHP

CakePHP 4 Log User Authentication Using Component

This tutorial will discuss how to create a user activities log using the CakePHP component. The basic activities that will be logged are the authentication method which includes the login and logout details.

3 min read