CodeIgniter Forums
Automatic database logging - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Automatic database logging (/thread-20879.html)

Automatic database logging - El Forum - 07-24-2009

Hi there,
i got a CI application in which i got many and many database insert, update, delete calls.
Now i want to establish a "user log database table", in which all actions of all users should be automatically logged, including:
- create a record
- delete a record
- update a record, and here: which field is updated - what are its old and new values?
- maybe anything more

I wonder, if there is a smart and easy CI way of doing this. Maybe there is an existing function or a library.
Or do i have to write an own library with hooks before DB calls?

I really do not know, where to start.
The most important thing is, that the log funtionality is in only one global method, which, in best case, writes those logs automatically based on the last database action(s).

Best regards,

Automatic database logging - El Forum - 07-24-2009

You would need to insert this code into a model, so that every time you perform a database action it would call that code within the model. If your models are fairly small you could just create a function which gets called every time and just pass int the string you want to write.

If you want to use it across many models, then it might be better to create a reusable class.

I just remembered: I actually created an application that loggs relavent user actions (inserts/updates/deletes etc...) to the database. To do this, I just created another table in the database (called it log). And then just inserted the timestamp and string.

I used transactions with the queries, as i had to perform 2 queries each time something happened.