CodeIgniter Forums

Full Version: replacing CI's log_message global function in common.php
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I would like to substitute CI's log_message function defined in common.php by my own version of that function with the same name so that all existing calls to log_message (Within CI or within my code) will continue to work.

The purpose is for the new log_message function to call my own logging solution which IS NOT an extension of the CI_Log Class. 

I know I can create a MY_Log Class and override the CI_Log Class, but if possible I would like to redirect the messaging right from the entry point (the log_message function).

Any suggestions on overriding CI global functions?

I am all ears  :-)
All functions in Common.php are wrapped with checks to see if the function exists. So the easiest way to override those methods is to modify index.php to load your file with the replacement functions. This will load your functions first, since the other functions are not loaded until the CodeIgniter.php file is loaded at the bottom of the index.php file.
Thank you Kilishan.

I really appreciate the time you took to respond and I tried your approach along with others I had tried.

Prior to reading your response I had tested out two solutions:
1 - Overriding CI_Log with my own MY_Log
2 - Creating a pre-system hook to declare my own log_message function there

After reading your post, I also examined solution #3:
3 - Declaring my own log_message in index.php since checks in common.php won't attempt to recreate an existing function.

------------------------------------------------- The results ---------------------------------------------------

Solution #1 - Overriding CI_Log with MY_Log   *** The winner ***
This appears to be the best option even if it means that logging passes thru log_message, MY_Log and then to my custom logging object. Since logging of messages starts quite early in the workflow this approach overrides the standard logging just when it is needed which also means late enough for resources such as Config values to be available. Furthermore, using MY_Log I was able to supply an additional value (source) when calling my custom logger in which I can indicate that these messages originate from calls to CI's standard log_message.

The only other drawback was that I could not use config items from my customconfig.php file since the first logging requests takes place before custom config files are loaded. So I added my custom entry into config.php.

This only demonstrates that CI's standard overriding options such as (MY Objects, Hooks, etc) are frequently the best way to go  :-)

Solution #2 - Using pre-system hook
This solution was not viable since pre-system hooks occur after common.php was read. So log_message was already defined by CI.

Solution #3 - Modifying index.php
This clearly permits declaring log_message before common.php tries to do so. But I quickly encountered new challenges. Since index.php is at the very start of every request, config items and class autoloading hasn't yet taken place. So initializing my logger there meant I had to use defines to act as config items which could later be used in the assignment of regular config items. And since Autoloading had not yet occurred, I could not use namespace and class name to reference my custom logging. I needed to include the class file before hand.