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

[eluser]falkencreative[/eluser]
Hello all. I've recently started using CodeIgniter, and like a lot of beginners (I imagine) I'm starting by writing a basic application. Basically, the site is split into two sections -- a client frontend where clients can log in and view pages specific to them, and an admin section for admin users to login and manage clients, client information, etc.

I'm looking to develop something that will keep track of recent actions by the admin. For example, I would keep a log of actions such as adding new clients, updating client information, uploading files, etc. Ideally, I will create some sort of function that will be accessible to all controllers, something along the lines of:

log($logType, $logMessage);

that would take the input and log it in the database.

Where would be the best place to put this code? Create a library and place it within the libraries folder? Create a controller for this? (I'm not sure how that would work -- I don't think that is the right way to go). Add it to the helper functions?

I'd appreciate any comments or links to applicable websites.
#2

[eluser]heavenquake[/eluser]
In MVC generally and CodeIgniter specifically you communicate with the database through models.

I would write a model and place it in the models folder.

Then I would do:
Code:
$this->load->model('logs');
$this->logs->save($log_type, $log_message);

when I need to save stuff and

Code:
$this->load->model('logs');
$logs = $this->logs->read($log_type); //this function should get all logs of the given type as an array

When I need to view stuff.

the code for the model would be like this:

Code:
class Logs extends Model {

   function save($log_type, $log_message)
   {
       //code to save in database
   }

   function read($log_type)
   {
       //code to read from database
   }

}

Have a look at the userguide pages on models ( http://ellislab.com/codeigniter/user-gui...odels.html ) and database access ( http://ellislab.com/codeigniter/user-gui...index.html ) for more info
#3

[eluser]falkencreative[/eluser]
Thanks -- I appreciate the reply.




Theme © iAndrew 2016 - Forum software by © MyBB