Header Ads

Seo Services


As usual, you might want to read the past tutorials:

For today, 

We're going to improve on log function, before i show you what i did... let me tell you why.

In case of a large and robust application it might be hard to trace error logs and success logs. When actions are taken, either successful or not, it should be logged... our present log function is only useful for logging error messages. And so its necessary to modify the function so it will do more than just log errors.

Now, here's what we have.

1. I changed the function name from log_error log_action, this is because we are doing more than just error log

2. I split the function into two..  (i like doing this because its much easier to debug smaller functions, and it makes my code more reusable and professional - my definition). 

The first one is log_action, which is basically for identifying the type of action to log (error or log), then the log_action calls the do_log, which does the actual logging.

3. I created a folder for error logs separately (still in the log folder). And our normal log is still in the general log folder.

** I do hope this is not getting confusing, if it is please drop a comment **

Function log_action - for checking the type of action to log, and calls function do_log.

Function do_log(), which is called after the log_action has identified the type of action.

$action : The action performed by the user
$message: Message returned after action is performed
$log_type: The type of log, at the time of this writing only two types are available - error and log
$errorfile: the actual file name and location
$datestamp: getting the present date time to allocate unique name to $errorfile 
$new_log : testing if the file already exist - return type is boolean

and now, you should get different sets of logs.

As time goes on, i might decide to improve on this function, if the need arises, but if you have any idea of improvement, please let me know.

My database class also changed, because we have to implement the edited function... now here's what i have:

Note that i am now passing 3 arguments in the log_action function which is different from what we had in the log_error function that only needs 2 arguments.

After making the changes, run your page and be sure there are no errors.

A few more changes to our open_connection method, 

This way,

We output error code to our users, they can contact us with the error code, we also log more details in our error file. (the details are self explanatory)

In my next post, we will do database input, user input sanitization, and probably apply a bit of Ajax instead of having our page reload every time.

If any part is unclear, please drop your questions... comments, suggestions or corrections.

Don't forget to share.

No comments