Welcome Guest, Not a member yet? Register   Sign In
Errors mailed vs. Logged
#1

[eluser]hanji[/eluser]
Hello All

Here is my first post on the CI forum! First off.. I'm really digging CI!!!

My question is this. Is there a way to have the errors emailed (including server variables) vs. just logged to the server? I've written my own error handling class in the past using set_error_handler(). I would like to not touch the core files, so I'm hoping if there is a way to extend a helper or some other technique to adjust how errors are handled.

Thanks!
hanji
#2

[eluser]pistolPete[/eluser]
Have a look at the user guide: Extending Core Class

Create a file MY_Log.php in ./system/application/libraries/ with the following contents:
Code:
class MY_Log extends CI_Log {
    
    private $CI;
    public function __construct()
    {
        //parent::CI_Input();
       // corrected:
        parent::CI_Log();
        $this->CI =& get_instance();
        $this->CI->load->library('email');
    }
    
    function write_log($level = 'error', $msg, $php_error = FALSE)
    {        
        if ($this->_enabled === FALSE)
        {
            return FALSE;
        }
    
        $level = strtoupper($level);
        
        if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
        {
            return FALSE;
        }
        
        // place your email code here to send $msg
    }
}
#3

[eluser]hanji[/eluser]
Hello

Thanks for the reply. I'm getting the following error...

Code:
Fatal error: Call to undefined method CI_Log::ci_input() in /var/www/localhost/htdocs/test/system/application/libraries/MY_Log.php on line 7

Thanks!
hanji
#4

[eluser]hanji[/eluser]
Doh.. I see the problem CI_Log vs ci_input. That's corrected, but it's complaining about Call to undefined function get_instance() now?

Thanks!
hanji
#5

[eluser]therealmaloy[/eluser]
@hanji

try this out, replace the function __construct(){} with this one

Code:
public function MY_Log()
    {
        parent::CI_Log();
        $this->CI =& get_instance();
        $this->CI->load->library('email');
    }
#6

[eluser]pistolPete[/eluser]
[quote author="hanji" date="1234771314"]Doh.. I see the problem CI_Log vs ci_input. That's corrected, but it's complaining about Call to undefined function get_instance() now?[/quote]

Are you using PHP4?
#7

[eluser]skunkbad[/eluser]
I may be a little late, but I use a hook:

Code:
<?php
function my_error_handling(){
    define('ADMIN_EMAIL', '[email protected]');
    function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
        if (stristr($_SERVER['HTTP_HOST'], 'localhost') || (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')) {
            $development_site = TRUE;
        }
        $message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
        $message .= "Date/Time: " . date('n-j-Y H:i:s') . "\n<br />";
        $message .= "<pre>" . print_r ($e_vars, 1) . "</pre>\n<br />";
        if ($development_site == TRUE) {
            echo '<p class="error">' . $message . '</p>';
        } else {
            error_log ($message, 1, ADMIN_EMAIL);
            if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
                echo '<p class="error">A system error occurred. We apologize for the inconvenience.</p>';
            }
        }
    }
    set_error_handler ('my_error_handler');
}
/* End of file my-error-handling.php */
/* Location: .system/application/hooks/my-error-handling.php */

Errors on my development server are shown on screen, Errors on the production server are emailed to me.
#8

[eluser]alive[/eluser]
I am having the same exact problem (and the same exact purpose). Using the following code:
Code:
class MY_Log extends CI_Log
{
    private $CI;

    public function MY_Log()
    {
        parent::CI_Log();
        $this->CI =& get_instance();
        $this->CI->load->library('email');
    }
...
}

Receiving error message as follows:
Fatal error: Call to undefined function get_instance() in /[site info]/system/application/libraries/MY_Log.php on line 16

Any help would be appreciated!




Theme © iAndrew 2016 - Forum software by © MyBB