Email log messages library in CodeIgniter

When working with CodeIgniter, I’ve found the log message functionality built into the framework very helpful. The other day I noticed in my log of one of my CodeIgniter based sites that I’ve had some 404 errors going on for some time. As I don’t have the time to check my logs daily, I hadn’t noticed this problem until now.

This led me to think that it would be real handy to get the log messages sent out by email as well, so you don’t risk to have a problem going on at the site unnoticed for days or even weeks.

So I wrote an extension to the native Log class in CodeIgniter which adds this functionality. Below is the code for MY_Log.php which extends the CI_Log class. MY_Log calls the native Log functions so the log file still gets generated, and if a log message was executed it also gets sent out by email afterward.

Change the email address in the code to the one you want to receive the log messages to, and then save the file in your application/libraries/ folder as MY_Log.php. This assumes that your Class Extension Prefix is set to MY_ in your config file.

I hope some of you find this class extension useful, and feel free to improve upon it. Cheers!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
 * MY_Log Class
 *
 * This library extends the native Log library.
 * It adds the function to have the log messages being emailed when they have been outputted to the log file.
 *
 * @package		CodeIgniter
 * @subpackage		Libraries
 * @category		Logging
 * @author		Johan Steen
 * @link		http://coding.cglounge.com/ 
 */
class MY_Log extends CI_Log {
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function MY_Log()
	{
		parent::CI_Log();
	}
 
	/**
	 * Write Log File
	 *
	 * Calls the native write_log() method and then sends an email if a log message was generated.
	 *
	 * @access	public
	 * @param	string	the error level
	 * @param	string	the error message
	 * @param	bool	whether the error is a native PHP error
	 * @return	bool
	 */	
	function write_log($level = 'error', $msg, $php_error = FALSE)
	{
		$result = parent::write_log($level, $msg, $php_error);
 
		if ($result == TRUE && strtoupper($level) == 'ERROR') {
			$message = "An error occurred: \n\n";
			$message .= $level.' - '.date($this->_date_fmt). ' --> '.$msg."\n";
 
			$to = 'someone@example.com';
			$subject = 'An error has occured';
			$headers = 'From: Example Name <no-reply@example.com>' . "\r\n";
			$headers .= 'Content-type: text/plain; charset=utf-8\r\n'; 
 
			mail($to, $subject, $message, $headers);
		}
		return $result;
	}
}
?>

Liked this post?

Subscribe to the site feed with RSS or by email.

Tags: , , , , ,
Category: CodeIgniter

Digg it Delicious Stumbleupon Spread the word on Twitter! Share on Facebook Share on LinkedIn

Comments

  1. jgJune 6, 2009

    It’s not working if you make a mistake in controller name in URL:
    Fatal error: Call to undefined function get_instance() in …/application/libraries/MY_Log.php on line 44

    Try http://example.com/wrong_controller_name

    ( Reply )
    1. JohanJune 6, 2009

      Hey,
      Thanks for the heads up, after some additional research the get_instance() function is not always available to exceptions in CodeIgniter. 404 being one of them.

      I updated the library to use the standard PHP mail instead and not rely on CodeIgniter’s mail library, to make sure it always works, no matter how much of CodeIgniter that has been initialized up until the exception occurs.

      Try the updated version, and it should work fine now with 404’s, otherwise let me know.

      Cheers!

      ( Reply )
  2. jgJune 6, 2009

    My proposition:

    if ($result == TRUE && strtoupper($level) == ‘ERROR’) { … }

    Email log only for ERROR, not for DEBUG and INFO

    ( Reply )
    1. JohanJune 6, 2009

      Cool proposition, I liked it, I haven’t touched the error levels very much yet in CI, but I guess I’ll start using the DEBUG level soon, and well, no need to flood the mailbox with those.

      So I added your proposition to the code in the post. Very nice, thanks! :)

      ( Reply )
  3. LukeSeptember 4, 2009

    This is a great idea to be proactive in catching issues on your CI websites. I did something similar and came across this blog when I was getting issues with 404 pages like jg mentioned.

    I took your suggestion to use the PHP mail function instead of the CI email class, but the other issue I came across is that I wanted to store things such as the email address to send exceptions to in my config file. I also have a config item to toggle emails on and off since I don’t want to get emails when I’m in development.

    The workaround I found was to use the load_class function to load the config class only, so I could access my config vaules. For example:

    $cfg =& load_class(‘Config’);
    $to = $cfg->config['exception_to_email_address'];
    $from = $cfg->config['exception_from_email_address'];

    …etc.

    ( Reply )

Leave a Reply

( Get a Gravatar )
  1. Gravatar

    Your Name
    March 11, 2010


Trackbacks and Pingbacks

( Leave a Trackback )