CodeIgniter Error Log Info + Errors
IS there a way to save in the log, Info + Errors without debug?
Howcome debug level apears with info? If i want to log info "Account id 4345 was deleted by Admin", why do i need to see all of these:DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Config Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Hooks Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 URI Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Router Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Output Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Input Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Global POST and COOKIE da开发者_开发知识库ta sanitized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Language Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Loader Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Config file loaded: config/safe_charge.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Config file loaded: config/web_fx.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Helper loaded: loadutils_helper
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Helper loaded: objectsutils_helper
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Helper loaded: logutils_helper
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Helper loaded: password_helper
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Database Driver Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 cURL Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Language Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Config Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Account MX_Controller Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 File loaded: ./modules/accounts/models/pending_account_model.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Model Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 File loaded: ./modules/accounts/models/proccess_accounts_model.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Model Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 File loaded: ./modules/accounts/models/web_fx_model.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Model Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 File loaded: ./modules/accounts/models/trader_account_type_spreads.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Model Class Initialized
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 File loaded: ./modules/accounts/models/trader_accounts.php
DEBUG - 2010-12-27 08:39:13 --> 192.168.200.32 Model Class Initialized
Thanks
You will not face the issue in codeigniter 2
$config['log_threshold'] = array(1, 3);
in config/config.php
and
log_message('info', 'The purpose of some variable is to provide some value.');
where you want to log something will only print that message to the log file, no excess debug code.
BUT
In codeIgniter 3, Many system core files have
log_message('info', ******************);
Like
log_message('info', 'Config Class Initialized');
log_message('info', 'Controller Class Initialized');
log_message('info', 'Hooks Class Initialized');
............
That's why when
$config['log_threshold'] = array(1, 3);
or
$config['log_threshold'] = array(3);
or
$config['log_threshold'] = 3;
then all the undesired log messages are appearing in log file.
So, the one way is to resolve this is to extend all core files and change
log_message('info'
to
log_message('debug'
or creating a new thresold, like
in config/config.php
$config['log_threshold'] = array(1, 5);
in core/MY_Log.php
protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4', 'USER_INFO' => '5');
Full core/MY_Log.php file
class MY_Log extends CI_Log
{
protected $_log_path;
protected $_threshold = 1;
protected $_date_fmt = 'Y-m-d H:i:s';
protected $_enabled = TRUE;
protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4', 'USER_INFO' => '5');
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$config =& get_config();
$this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
$this->_file_ext = (isset($config['log_file_extension']) && $config['log_file_extension'] !== '')
? ltrim($config['log_file_extension'], '.') : 'log';
file_exists($this->_log_path) OR mkdir($this->_log_path, 0755, TRUE);
if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
{
$this->_enabled = FALSE;
}
if (is_numeric($config['log_threshold']))
{
$this->_threshold = (int) $config['log_threshold'];
}
elseif (is_array($config['log_threshold']))
{
$this->_threshold = 0;
$this->_threshold_array = array_flip($config['log_threshold']);
}
if ( ! empty($config['log_date_format']))
{
$this->_date_fmt = $config['log_date_format'];
}
if ( ! empty($config['log_file_permissions']) && is_int($config['log_file_permissions']))
{
$this->_file_permissions = $config['log_file_permissions'];
}
}
// --------------------------------------------------------------------
/**
* Write Log File
*
* Generally this function will be called using the global log_message() function
*
* @param string the error level
* @param string the error message
* @param bool whether the error is a native PHP error
* @return bool
*/
public 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))
&& ! isset($this->_threshold_array[$this->_levels[$level]]))
{
return FALSE;
}
if($level == 'USER_INFO')
{
$filepath = $this->_log_path . 'info-log-' . date('Y-m-d') . '.php';
}
else
{
$filepath = $this->_log_path . 'log-' . date('Y-m-d') . '.php';
}
$message = '';
if ( ! file_exists($filepath))
{
$newfile = TRUE;
// Only add protection to php files
if ($this->_file_ext === 'php')
{
$message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
}
}
if ( ! $fp = @fopen($filepath, 'ab'))
{
return FALSE;
}
// Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
if (strpos($this->_date_fmt, 'u') !== FALSE)
{
$microtime_full = microtime(TRUE);
$microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000);
$date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short, $microtime_full));
$date = $date->format($this->_date_fmt);
}
else
{
$date = date($this->_date_fmt);
}
$message .= $level.' - '.$date.' --> '.$msg."\n";
flock($fp, LOCK_EX);
for ($written = 0, $length = strlen($message); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($message, $written))) === FALSE)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
if (isset($newfile) && $newfile === TRUE)
{
chmod($filepath, $this->_file_permissions);
}
return is_int($result);
}
}
I realize this question is rather old, but I ran into the same problem myself and was able to modify the Log library to fix it.
modify the write_log function in Log.php as follows:
public function write_log($level = 'error', $msg, $php_error = FALSE)
{
if ($this->_enabled === FALSE)
{
return FALSE;
}
$level = strtoupper($level);
if ( ! isset($this->_levels[$level]))
{
return FALSE;
}
if (($this->_levels[$level] > $this->_threshold) OR (is_array($this->_threshold) && !in_array($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;
}
You're then able to specify an array in your config file:
$config['log_threshold'] = array(3, 1);
And then your log file will look something like this:
ERROR - 2013-07-24 13:50:04 --> Severity: Warning -->
INFO - 2013-07-24 13:50:04 --> The purpose of some variable is to provide some value.
ERROR - 2013-07-24 13:50:06 --> 404 Page Not Found --> 404.shtml
quickfix tested on codeigniter 2.1.4
copy
/system/libary/log.php
to
/application/libary/log.php
add following lines before $filepath (line 86)
// remove all DEBUG messages
if ( $this->_levels[$level] == 2 )
{
return FALSE;
}
From CodeIgniter's docs:
log_message('info', 'The purpose of some variable is to provide some value.');
Informational Messages. These are the lowest priority messages, simply giving information regarding some process. CodeIgniter doesn't natively generate any info messages but you may want to in your application.
Note: In order for the log file to actually be written, the "logs" folder must be writable. In addition, you must set the "threshold" for logging. You might, for example, only want error messages to be logged, and not the other two types. If you set it to zero logging will be disabled.
a better way i will sugeest is to use the file sugested by codeigniter. I had the same problem and it foxed it. check it out here https://github.com/EllisLab/CodeIgniter/wiki/Custom-Logging-Threshold
精彩评论