Reputation: 205
I'm trying to customize the logging functionality of codeigniter. I found this forum thread which describes exactly what I need: http://codeigniter.com/forums/viewthread/139997/ But I want to do that without altering core files/classes.
I"m trying to alter the log_message function. I've already extended the CI_Log class and that is working well, but now I'm trying to alter log_message() which resides in system/core/Common.php. It isn't actually a class so I can't extend it, it just appears to be a collection of useful functions. I tried redeclaring log_message() and placing it in application/core/Common.php, but that doesn't appear to work. Any ideas how I can go about this?
Upvotes: 1
Views: 2237
Reputation: 1222
A helper function could work here. You'd have to use it instead of log_message().
Here's what I did:
in application/helpers I created 'new_log_helper.php' (helpers have to end in _helper.php) I wanted this to add log entries into the database and to track by process (that way I know where to look in the code).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Custom Logging Interface
*
* Add log entries to a log table on the db and track process name
*
* @access public
* @return void
*/
if ( ! function_exists('new_log')) {
function new_log($level = 'error', $message, $process, $php_error = FALSE)
{
if (config_item('log_threshold') == 0) {
return;
}
$CI =& get_instance();
$CI->load->model('new_log','logger', TRUE);
$CI->logger->add_event($process, $level, $message);
log_message($level, $process.': '.$message, $php_error);
}
}
Then when you need it use it like this:
$this->load->helper('new_log');
...
new_log('debug','Something happened','Stack overflow answer');
Anyway I know your issue was years ago but I was looking so maybe someone else is too.
Upvotes: 0
Reputation: 1870
I believe that the function you actually want to alter is write_log()
, correct me if I'm wrong. You can do this by extending the function within application\libraries\MY_Log.php
.
I have the following script in my MY_Log.php which emails me any time an error is thrown:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Extends the logging class to send an email when an error is triggered */
class MY_Log extends CI_Log {
function __construct()
{
parent::__construct();
}
function write_log($level = 'error', $msg, $php_error = FALSE, $additional, $additional2)
{
if ($this->_enabled === FALSE)
{
return FALSE;
}
$level = strtoupper($level);
if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
{
return FALSE;
}
$filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
$message = '';
if ( ! file_exists($filepath))
{
$message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
}
if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
{
return FALSE;
}
$message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
@chmod($filepath, FILE_WRITE_MODE);
return TRUE;
}
}
If you're not looking to extend the write function, you will need to extend one of the other log functions.
Upvotes: 1