Meet “The Rat”, my CodeIgniter logger library

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:

$config['store_in'] = 'database'; - You can choose to keep the logs inside the database or in a specific directory (relative to the APPPath). If you leave blank (''), it will assume the 'application/logs' directory will be used

$config['session_user_id'] = 'user_id'; - If you want to pass the user's ID automatically to the library when creating the log, you can setup a session value and pass it to this config item. If not, leave blank.

$config['table_name'] = ''; - If you wanted to be original and prefered another table name, you must change it here. Leave blank if the table name is 'rat'

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”…):

DROP TABLE IF EXISTS `rat`;
CREATE TABLE `rat` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`date_time` datetime DEFAULT NULL,
`code` int(11) DEFAULT NULL,
`message` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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

$this->rat->log('This is a message that will be logged');

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:

$this->rat->log('This is a message that will be logged', 5);

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”.

$this->rat->log('This is the success message',0,1);

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

$this->rat->get_log(); //this is if you want the general log
$this->rat->get_log(1); // this is if you want a specific user's log
$this->rat->get_log(1,1); // this is if you want a specific user's log, but only the success messages
$this->rat->get_log(NULL,NULL,'2015-06-29'); // this is if you want the logs from a specific date

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 comments

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

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

  2. 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.

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

Leave a Reply

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

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