Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Audit Log Plugin

Asyraf Wahi Anuar 3 min read

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.

This tutorial may be outdated

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

Tech stack

  • CakePHP 4.x

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. Rather than acting as evidence, audit logs are also used to support versioning for each content. To integrate the audit trails in CakePHP based web application, a CakePHP Audit Log Plugin can be used.

Repository URL: https://github.com/hevertonfreitas/cakephp-audit-log

Installation

Download the plugin using composer:

Terminal
composer require hevertonfreitas/cakephp-audit-log

Load Plugin

Terminal
bin/cake plugin load AuditLog

or manually load the plugin in .../src/Application.php.

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

Database Table Migration

Execute the following command to run the table migration. This process will generate two tables i)audits; ii)audit_deltas.

Terminal
bin/cake migrations migrate -p AuditLog

or manually create the table:

PHP
CREATE TABLE `audits` (
  `id` char(36) NOT NULL,
  `event` varchar(255) NOT NULL,
  `model` varchar(255) NOT NULL,
  `entity_id` varchar(255) NOT NULL,
  `json_object` text DEFAULT NULL,
  `description` text DEFAULT NULL,
  `source_id` varchar(255) DEFAULT NULL,
  `created` datetime NOT NULL,
  `delta_count` int(11) NOT NULL DEFAULT 0,
  `source_ip` varchar(255) DEFAULT NULL,
  `source_url` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `audit_deltas` (
  `id` char(36) NOT NULL,
  `audit_id` char(36) NOT NULL,
  `property_name` varchar(255) NOT NULL,
  `old_value` text DEFAULT NULL,
  `new_value` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `audits`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `audit_deltas`
  ADD PRIMARY KEY (`id`);

AuditLog Behavior

Add AuditLog behaviour in your specific model. In this tutorial, I used the Article table which can be found at .../src/Model/Table/ArticlesTable.php

src/Model/Table/ArticlesTable.phpPHP
public function initialize(array $config): void
{
    parent::initialize($config);

    ...
    
    $this->addBehavior('AuditLog.Auditable', [
        'ignore' => ['created'],
        //'habtm' => ['Tags'],
    ]);
}

At this point, the AuditLog will capture all changes made to the contents.

To capture the id, ip, URL and description, add the following code in the model:

PHP
use Cake\Routing\Router; //at top of your model table
...
public function currentUser(): array
{
    return [
        'id' => '15',
        'ip' => Router::getRequest()->clientIp(),
        'url' => Router::url(null, true),
        'description' => 'Testing audit log',
    ];
}

For easy references, bake the audits table so that you can view the audit log.

That's all. Happy coding :)

Did this tutorial help?

269 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 Trail Using AuditStash Plugin

This plugin adds an "audit trail" to any of your Table classes in your application, allowing the web applications to record the creation, change, or deletion of any table's entities. In theory, it will store the old data before replacing it with the new data or content.

3 min read

CakePHP

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.

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