[eluser]Gavin Vickery[/eluser]
I've been working on a client system where they want certain details, such as logins, logouts and system changes to be logged.
I was using the 'INFO' (4) threshold setting for the CI native log function, but noticed that it will also log any threshold less than whats set. So if you have a log threshold of 3, 1, 2 and 3 will be logged.
Since the client only wants 'Footprints' logged, I decided to tweak the Log library for CodeIgniter to only show this specific log type when used, instead of displaying them all.
The type I set it to is 'FOOTPRINT' (5)
Here are the steps to do it:
Step 1: Mod the CI Log Library
In the Log.php file located in the libraries directory change the line:
Code:
var $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
To:
Code:
var $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4', 'FOOTPRINT' => '5');
Then replace your
write_log function with:
Code:
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;
}
// EDIT TO SHOW ONLY FOOTPRINT
if($this->_threshold == 5)
{
if($this->_levels[$level] < 5)
{
return FALSE;
}
}
$filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
$message = '';
if ( ! file_exists($filepath))
{
$message .= "<"."?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
}
if ( ! $fp = @fopen($filepath, "a"))
{
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, 0666);
return TRUE;
}
Basically, we are just adding in:
Code:
// EDIT TO SHOW ONLY FOOTPRINT
if($this->_threshold == 5)
{
if($this->_levels[$level] < 5)
{
return FALSE;
}
}
This just tells the function to log only 'FOOTPRINTS' when the Footprints threshold is set. That way we aren't effecting how CI core logging functionality works.
Step 2: Set the Threshold
Set the following line in the config.php file:
Code:
$config['log_threshold'] = 5;
Step 3: Loggin Footprints
Now you can log footprints just like any other log type in CI
Code:
log_message('footprint', 'User, John Doe, logged in successfully');
I'm not sure if anybody else could use this, but I figured I would post it anyway.