PHPackages                             robwilkerson/cakephp-audit-log-plugin - 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. robwilkerson/cakephp-audit-log-plugin

ActiveCakephp-plugin[Logging &amp; Monitoring](/categories/logging)

robwilkerson/cakephp-audit-log-plugin
=====================================

Audit Plugin for CakePHP

2.30(7y ago)10167.8k↓33.3%75[8 issues](https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin/issues)[3 PRs](https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin/pulls)MITPHPPHP &gt;=5.3.0

Since Jan 28Pushed 7y ago12 watchersCompare

[ Source](https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin)[ Packagist](https://packagist.org/packages/robwilkerson/cakephp-audit-log-plugin)[ Docs](https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin)[ RSS](/packages/robwilkerson-cakephp-audit-log-plugin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (9)Used By (0)

Audit Log Plugin
================

[](#audit-log-plugin)

A logging plugin for [CakePHP](http://cakephp.org) 2.x. The included `AuditableBehavior` creates an audit history for each instance of a model to which it's attached.

The behavior tracks changes on two levels. It takes a snapshot of the fully hydrated object *after* a change is complete and it also records each individual change in the case of an update action.

Features
--------

[](#features)

- Support for CakePHP 2.2+. Thanks, @jasonsnider.
- Tracks object snapshots as well as individual property changes.
- Allows each revision record to be attached to a source -- usually a user -- of responsibility for the change.
- Allows developers to ignore changes to specified properties. Properties named `created`, `updated` and `modified` are ignored by default, but these values can be overwritten.
- Handles changes to HABTM associations.
- Fully compatible with the [`PolymorphicBehavior`](https://github.com/cakephp/cookbook/blob/master/models/behaviors/polymorphic.php).
- Fully compatible with [`SoftDelete`](https://github.com/CakeDC/utils).

Versions
--------

[](#versions)

### CakePHP 2.x

[](#cakephp-2x)

Use the current master branch and follow the instructions below.

### CakePHP 3.x

[](#cakephp-3x)

The `dev-3.x` branch is now dedicated to any CakePHP 3.x development but is still WIP. If you are looking for working solutions you can look at the plugins listed at [Awesome CakePHP](https://github.com/FriendsOfCake/awesome-cakephp)

### CakePHP 1.3.x

[](#cakephp-13x)

Use code from the `1.3` branch and follow the instructions in that README file. Please note that development has ended for this version.

Installation (2.x)
------------------

[](#installation-2x)

### Installation Options

[](#installation-options)

#### Using Composer

[](#using-composer)

From your `/app/` folder run:

```
$ composer require robwilkerson/cakephp-audit-log-plugin
```

This will automatically update your `composer.json`, download and install the required packages.

#### As an Archive

[](#as-an-archive)

1. Click the big ol' **Downloads** button next to the project description.
2. Extract the archive to `app/Plugin/AuditLog`.

#### As a Submodule

[](#as-a-submodule)

1. `$ git submodule add git://github.com/robwilkerson/CakePHP-Audit-Log-Plugin.git /app/Plugin/AuditLog`
2. `$ git submodule init`
3. `$ git submodule update`

### Load the Plugin

[](#load-the-plugin)

In `app/Config/bootstrap.php` add the following line:

```
CakePlugin::load('AuditLog');
```

### Setup Database

[](#setup-database)

To create the necessary tables, you can use the schema shell. Run from your /app/ folder:

```
php ./Console/cake.php schema create -p AuditLog

```

This will create the `audits` and `audit_deltas` tables that will store each object's relevant change history.

### Next Steps

[](#next-steps)

1. Create a `currentUser()` method, if desired.

    The `AuditableBehavior` optionally allows each changeset to be "owned" by a "source" -- typically the user responsible for the change. Since user and authentication models vary widely, the behavior supports a callback method that should return the value to be stored as the source of the change, if any.

    The `currentUser()` method must be available to every model that cares to track a source of changes, so I recommend that a copy of CakePHP's `AppModel.php` file be created and the method added there. Keep it DRY, right?

    Storing the changeset source can be a little tricky if the core `Auth` component is being used since user data isn't readily available at the model layer where behaviors lie. One option is to forward that data from the controller. One means of doing this is to include the following code in `AppController::beforeFilter()`:

    ```
    if (!empty($this->request->data) && empty($this->request->data[$this->Auth->userModel])) {
    	$user['User']['id'] = $this->Auth->user('id');
    	$this->request->data[$this->Auth->userModel] = $user;
    }
    ```

    The behavior expects the `currentUser()` method to return an associative array with an `id` key. It is also possible to set a `description` key to add additional details about the user. Continuing from the example above, the following code might appear in the `AppModel`:

    ```
    /**
     * Get the current user
     *
     * Necessary for logging the "owner" of a change set,
     * when using the AuditLog behavior.
     *
     * @return mixed|null User record. or null if no user is logged in.
     */
    public function currentUser() {
        App::uses('AuthComponent', 'Controller/Component');
        return array(
            'id' => AuthComponent::user('id'),
            'description' => AuthComponent::user('username'),
        );
    }
    ```
2. Attach the behavior to any desired model and configure it to your needs.

Configuration
-------------

[](#configuration)

Applying the `AuditableBehavior` to a model is essentially the same as applying any other CakePHP behavior. The behavior does offer a few configuration options:

- `ignore` An array of property names to be ignored when records are created in the deltas table.
- `habtm` An array of models that have a HABTM relationship with the acting model and whose changes should be monitored with the model. If the HABTM model is auditable in its own right, don't include it here. This option is for related models whose changes are *only* tracked relative to the acting model.

```
/**
 * A model that uses the AuditLog default settings
 */
class SomeModel extends AppModel {

	/**
	 * Loading the AuditLog behavior without explicit settings.
	 *
	 * @var array
	 */
	public $actsAs = array('AuditLog.Auditable');

	// More model code here.
}

/**
 * A model that sets explicit AuditLog settings
 */
class AnotherModel extends AppModel {

	/**
	 * Loading the AuditLog behavior with explicit settings.
	 *
	 * @var array
	 */
	public $actsAs = array(
		'AuditLog.Auditable' => array(
			'ignore' => array('active', 'name', 'updated'),
			'habtm' => array('Type', 'Project'),
		)
	);

	// More model code here.
}
```

### Callbacks

[](#callbacks)

The plugin offers multiple [Callbacks](http://book.cakephp.org/2.0/en/models/callback-methods.html) that allow the execution of additional logic before or after an operation of this Plugin.

To add a custom Callback create a method with one of the names listed below in your audited model.

#### afterAuditCreate

[](#afterauditcreate)

###### afterAuditCreate(Model $Model)

[](#afterauditcreatemodel-model)

Triggers if a new record is inserted in the Audit table

- `$Model` Name of the model where the record is created

#### afterAuditUpdate

[](#afterauditupdate)

###### afterAuditCreate(Model $Model, array $original, array $updates, int $auditId)

[](#afterauditcreatemodel-model-array-original-array-updates-int-auditid)

Triggers if a record is updated in the Audit table.

- `$Model` Name of the model where record is updated
- `$original` Contains a copy of the object as it existed prior to the save
- `$updates` Set of records that have been updated
- `$auditId` Id of the record in the `Audit` table

#### afterAuditProperty

[](#afterauditproperty)

###### afterAuditCreate(Model $Model, string $propertyName, string $oldValue, string $newValue)

[](#afterauditcreatemodel-model-string-propertyname-string-oldvalue-string-newvalue)

Triggers each time a property is inserted into the `audit_deltas` table.

- `$Model` Name of the model where record is created / updated
- `$propertyName` Name of the property (field name)
- `$oldValue` Original value of the property
- `$newValue` New value of the property

License
-------

[](#license)

This code is licensed under the [MIT license](http://www.opensource.org/licenses/mit-license.php).

Notes
-----

[](#notes)

Feel free to submit bug reports or suggest improvements in a ticket or fork this project and improve upon it yourself. Contributions welcome.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~161 days

Recently: every ~212 days

Total

7

Last Release

2647d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/71c36c66e884c9dfc827efd40666547be3a5c38113905a8543977283fedf400b?d=identicon)[robwilkerson](/maintainers/robwilkerson)

---

Top Contributors

[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (62 commits)")[![robwilkerson](https://avatars.githubusercontent.com/u/7735?v=4)](https://github.com/robwilkerson "robwilkerson (42 commits)")[![jasonsnider](https://avatars.githubusercontent.com/u/209596?v=4)](https://github.com/jasonsnider "jasonsnider (14 commits)")[![inigoflores](https://avatars.githubusercontent.com/u/5518087?v=4)](https://github.com/inigoflores "inigoflores (4 commits)")[![wuilliam321](https://avatars.githubusercontent.com/u/236566?v=4)](https://github.com/wuilliam321 "wuilliam321 (4 commits)")[![wewilkinson](https://avatars.githubusercontent.com/u/4845490?v=4)](https://github.com/wewilkinson "wewilkinson (2 commits)")[![ptica](https://avatars.githubusercontent.com/u/169873?v=4)](https://github.com/ptica "ptica (1 commits)")[![rchavik](https://avatars.githubusercontent.com/u/39490?v=4)](https://github.com/rchavik "rchavik (1 commits)")[![paulleephp](https://avatars.githubusercontent.com/u/2199146?v=4)](https://github.com/paulleephp "paulleephp (1 commits)")[![CodeLingoBot](https://avatars.githubusercontent.com/u/45469328?v=4)](https://github.com/CodeLingoBot "CodeLingoBot (1 commits)")[![CpuID](https://avatars.githubusercontent.com/u/916201?v=4)](https://github.com/CpuID "CpuID (1 commits)")[![jlpuder](https://avatars.githubusercontent.com/u/5078486?v=4)](https://github.com/jlpuder "jlpuder (1 commits)")[![josephsong](https://avatars.githubusercontent.com/u/319111?v=4)](https://github.com/josephsong "josephsong (1 commits)")[![chrisjohnson](https://avatars.githubusercontent.com/u/178144?v=4)](https://github.com/chrisjohnson "chrisjohnson (1 commits)")

---

Tags

logcakephpAuditaudit-trailauditlog

### Embed Badge

![Health badge](/badges/robwilkerson-cakephp-audit-log-plugin/health.svg)

```
[![Health](https://phpackages.com/badges/robwilkerson-cakephp-audit-log-plugin/health.svg)](https://phpackages.com/packages/robwilkerson-cakephp-audit-log-plugin)
```

###  Alternatives

[sammaye/yii2-audittrail

A port of audit trail

39408.2k](/packages/sammaye-yii2-audittrail)[sandreu/cake-sentry

Sentry error handler plugin for CakePHP2

1640.2k](/packages/sandreu-cake-sentry)[msonowal/laravel-auditor

A simple mongo activity logger to record various events of your laravel application

1030.2k1](/packages/msonowal-laravel-auditor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
