Welcome Guest, Not a member yet? Register   Sign In
logging to a database
#1

[eluser]fatTireFreak[/eluser]
I'm new to CI and I want to extend the log class and put my logs in a database. I want to track errors as well as usage data and query it. Depending on traffic I'll move the data out and truncate the log table to keep it from getting too large.

I created a MY_Log.php library and added my own write_log function into it. It basically seems like a simple task to me, but I can't get the CI instance in MY_Log.php. If I do $CI =& get_instance(); I get:

Fatal error: Call to undefined function get_instance()

I'm guessing that the core functionality hasn't loaded yet when I hit write_log. If I reduce the logging threshold so that it doesn't log debug messages and move the get_instance call down below this line:

Code:
if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
        {
            return FALSE;
        }

Has anyone hit this issue and or found a work around.

Thanks in advance.
#2

[eluser]Buso[/eluser]
Great idea, I will add this to my to-do list

Yes, your guessing makes sense. You could make a separate function like log_db(), which you could use once the db is loaded. Previous messages you could fetch them somehow and move them to the db at that point.

Or write the queries in plain php/sql, and have all the messages logged that way.
#3

[eluser]fatTireFreak[/eluser]
This has turned into a bit of a rabbit hole. I could just code a db connection and query info right into MY_Log.php, but then if we ever change the database someone would have to go in there and change it or the whole app would break. So, I wanted to put the database information in a custom config file. Then I load that config file in the autoloader. But, that config isn't available in MY_Log.php at the time I need it.

I could put the db info in the config.php file, but I'm trying to keep everything that's specific to our implementation in one place.

So, I'm back to where I started. I really want to use the CI DB class and not code the connection and queries in the conventional way. I'm wondering if there's a way to use a hook? Or any suggestions that might help?

Thanks
#4

[eluser]TornUp[/eluser]
[quote author="fatTireFreak" date="1275014452"]This has turned into a bit of a rabbit hole. I could just code a db connection and query info right into MY_Log.php, but then if we ever change the database someone would have to go in there and change it or the whole app would break. So, I wanted to put the database information in a custom config file. Then I load that config file in the autoloader. But, that config isn't available in MY_Log.php at the time I need it.

I could put the db info in the config.php file, but I'm trying to keep everything that's specific to our implementation in one place.

So, I'm back to where I started. I really want to use the CI DB class and not code the connection and queries in the conventional way. I'm wondering if there's a way to use a hook? Or any suggestions that might help?

Thanks[/quote]

Hi,

Are you not able to simple code a plain PHP mysql insert file, and simply include the DB config file in that is used within CI (with a require_once(); ) and just pull the connection details from the config array? that way, when/if you change DB details, the CI DB config will have to be updated anyway?

maybe iv got the wrong end of the stick?! lol
#5

[eluser]Billy Shall[/eluser]
Ended up taking the simple route on this:

Code:
class MY_Log extends CI_Log {
    
    function __construct()
    {
        parent::CI_Log();
        
        $config =& get_config();
        
        if (is_numeric($config['log_threshold']))
        {
            $this->_threshold = $config['log_threshold'];
        }
    }

    function write_log($level = 'error', $msg, $php_error = FALSE)
    {        
        $level = strtoupper($level);
        
        if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
        {
            return FALSE;
        }

        $time = time();
        $ip = $this->get_ip_address();
        
        //Log Error To Database
        require(APPPATH.'config/database.php');

        mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password'])
            or die(mysql_error());
        
        mysql_select_db($db['default']['database'])
            or die(mysql_error());
        
        mysql_query("
            INSERT INTO errors (error_level, error_msg, error_time, error_ip_address)
            VALUES('$level', '$msg', '$time', '$ip')")
            or die(mysql_error());  
        
        mysql_close();
        
        return TRUE;
    }
    
    function get_ip_address()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))
        {
            $ip =  $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        elseif (!empty($_SERVER['REMOTE_ADDR']))
        {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        else
        {
            $ip = NULL;
        }        
        return $ip;
    }
}
#6

[eluser]patrick24601[/eluser]
n00b question - where do you put this class to extend a base class? Which folder? Thanks.
#7

[eluser]Billy Shall[/eluser]
[quote author="patrick24601" date="1287274462"]n00b question - where do you put this class to extend a base class? Which folder? Thanks.[/quote]

In libraries named MY_Log.php

More details here: http://ellislab.com/codeigniter/user-gui...aries.html

See the "Extending Native Libraries" section
#8

[eluser]patrick24601[/eluser]
Thank you




Theme © iAndrew 2016 - Forum software by © MyBB