Meet “The Rat”, my CodeIgniter logger library

Created at: June 29, 2015; Last update: June 29, 2015

Well… it’s been a while since I’ve written on my blog, but at least I came with a new library. This time is about reporting user actions to the developer. So, I named it “Rat” (slang for “A snitch. A person who has tattled on another person”).

Of course this is only a CodeIgniter logger, and not a “person”. You can find the library at the following Github repository: https://github.com/avenirer/CodeIgniter-Rat

This library will allow you to log everything you want to, inside a database or a file.

The installation of the library is quite easy. You simply copy all the files inside the according application directories, and then go to the “application/config/rat.php”. In there you will find out that you can set some configuration:

If you decided to use a database for the logs, this is how the sql command should look like (I sure hope you don’t have a table named “rat”…):

The usage is pretty simple. After you load the library you can write a log by using the log() method:

Now, if you also want to pass the message a user ID, you can either let the library get the user’s ID from a session variable that can be set inside the configuration file ($config[‘session_user_id’] = ‘user_id’;) or you can pass it as a second parameter to the log() method:

Also, I thought it to be useful for the library to allow you to set a different message code for your messages, whatever that code is. This way, you can set the error messages a code of “0”, and the success messages a code of “1”, for example. Then, when you output the logs you will know how to apply styling depending on the messages’ codes.

You can pass the code as a third parameter. If you’ve chosen for the library to take the user ID from the session variable, you set the second parameter as “0”.

To retrieve the logs, you use the get_log() method:

If you need more options you can find out about it in readme of the Github Repository.

You can also delete the log by using delete_log() method, which can accept a user ID and/or a date.

I sure hope you will give it a try and tell me what you think about it.

PS: If you need a slimmer (and therefore a faster) library, you can try a helper created by Tim Joosten, User Logger CodeIgniter (https://github.com/Tjoosten/User-logger-codeigniter).

5 thoughts on “Meet “The Rat”, my CodeIgniter logger library

  1. Federico

    Nice script…I realley need it. How about using this script with Ion-Auth library? for example using user_id of the Ion table.

    Reply
    1. avenirer Post author

      You can pass the user_id to a session variable and mention the session variable to the configuration file.

      Reply
  2. Wouter Koelewijn

    I’m using “userid” instead of “user_id” in my session. I modified the config file for Rat, but when logging events, the user_id field was still populated with 0.
    I found that in the Rat library, there’s this line:
    $user_id = isset($_SESSION[‘user_id’]) ? $_SESSION[‘user_id’] : ‘0’;
    The ‘user_id’ key ought to be replaced by $session_user_id, which retrieves the correct session field name from the config file.

    Reply
  3. Federico

    Hi Adrian, since Rat could returnmany rows logged into the DB…is there an easy way to add pagination to the library?
    Thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.