Welcome Guest, Not a member yet? Register   Sign In
Object Logging Library
#1

Hi all! I'd like to share my first completely original CodeIgniter4 Library. While my other libraries so far have been mostly ports from CI3, this one was conceived and implemented entirely from working in CI4. It's another lightweight library designed to "plug and play" so you don't have to worry about how to set it up or spend a lot of time configuring.
Presenting:


Tatter/Audits - out-of-the-box object event logging for CodeIgniter 4

Basic usage:
1. Install with Composer: `> composer require tatter/audits`
2. Update the database: `> php spark migrate:latest -all`
3. Setup your models:
PHP Code:
class JobModel extends Model
{
use \
Tatter\Audits\Traits\AuditsTrait;
protected 
$afterInsert = ['auditInsert'];
protected 
$afterUpdate = ['auditUpdate'];
protected 
$afterDelete = ['auditDelete']; 
4. All done!

Audits will watch for insert, update, and delete calls (or whichever you include) on configured models and log it in the `audits` table:
Code:
| id | source | source_id | user_id | event  | summary  |          created_at |
+----+--------+-----------+---------+--------+----------+---------------------+
| 10 | sites  |        27 |       9 | create | 2 rows   | 2019-04-05 15:58:40 |
| 11 | jobs   |        10 |       9 | update | 5 rows   | 2019-04-05 16:01:35 |
(Logs are batched to optimize database calls.)


I am eager for feedback, suggestions, and contributions - feel free to respond here or on GitHub (https://github.com/tattersoftware/codeigniter4-audits).
Thanks for reading!
Reply
#2

Would be more useful if it would log the actual fields and value of the changes
Reply
#3

@Inc33 I will look into it. At the time of this module the afterInsert and afterUpdate events didn’t contain sufficient data without making a second database call, which would be a big performance hit. The events have since changed so I will see how viable that is.
Reply
#4

(06-29-2020, 04:17 PM)MGatner Wrote: @Inc33 I will look into it. At the time of this module the afterInsert and afterUpdate events didn’t contain sufficient data without making a second database call, which would be a big performance hit. The events have since changed so I will see how viable that is.

You'r right, it's a dangerous path. But it is possible to do it, even if only contains the new values, as you can somehow get back the older values from the previous entry.
But if you have that, then you can basically create a trigger based event system in your app, and do a lot of cool stuff Big Grin
Reply
#5

(06-29-2020, 04:17 PM)MGatner Wrote: @Inc33 I will look into it. At the time of this module the afterInsert and afterUpdate events didn’t contain sufficient data without making a second database call, which would be a big performance hit. The events have since changed so I will see how viable that is.


I love the lib thus far, maybe this could be a option you could set in the model class. Like simple log or log differences. This way this could be done case by case.
Website: marcomonteiro.net  | Blog: blog.marcomonteiro.net | Twitter: @marcogmonteiro | TILThings: tilthings.com
Reply
#6

Thanks friends, really good feedback here! This module is rather old (in CI4 terms) and could definitely benefit from a fresh pass and some updated features. I will post an update soon.
Reply
#7

(This post was last modified: 12-03-2021, 02:55 AM by dgvirtual.)

(06-30-2020, 06:36 PM)MGatner Wrote: Thanks friends, really good feedback here! This module is rather old (in CI4 terms) and could definitely benefit from a fresh pass and some updated features. I will post an update soon.
Looks like a great library.

I need to log not only the fact of the save itself, but also the data passed to the database... Do you think your librarycould be extended to perform such a task?
==

Donatas G.
Reply
#8

(This post was last modified: 05-22-2023, 06:25 AM by Stack News.)

I needed to adapt audits for use with shield and I had no other chance than to do it this way
PHP Code:
<?php

namespace Config;

/***
*
* This file contains example values to alter default library behavior.
* Recommended usage:
* 1. Copy the file to app/Config/Audits.php
* 2. Change any values
* 3. Remove any lines to fallback to defaults
*
***/

class Audits extends \Tatter\Audits\Config\Audits
{
    function __construct()
    {
        parent::__construct();
        $session = \Config\Services::session();
        $session->set(['logged_in' => session('user')['id'] ?? 0]);
    }

    // Session key in that contains the integer ID of a logged in user
    public $sessionUserId 'logged_in';

    // Whether to continue instead of throwing exceptions
    public $silent false;


Just find this way to use tattersoftware/codeigniter4-audits with shield included
Please I hope that audits for shield is updated
Reply




Theme © iAndrew 2016 - Forum software by © MyBB