首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter错误日志信息+错误

CodeIgniter错误日志信息+错误
EN

Stack Overflow用户
提问于 2010-12-27 14:42:16
回答 5查看 12.9K关注 0票数 7

有没有一种方法可以在不调试的情况下保存日志,信息+错误?

如何了解调试级别与信息的关系?

如果我想记录“帐户id 4345已被管理员删除”的信息,为什么我需要查看所有这些信息:

代码语言:javascript
复制
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 data 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

谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-12-28 01:46:19

来自CodeIgniter's docs

代码语言:javascript
复制
log_message('info', 'The purpose of some variable is to provide some value.');

信息性消息。这些是优先级最低的消息,只是给出了有关某个进程的信息。CodeIgniter本身并不生成任何info消息,但您可能希望在您的应用程序中生成。

注意:为了实际写入日志文件,"logs“文件夹必须是可写的。此外,您还必须设置日志记录的“阈值”。例如,您可能只希望记录错误消息,而不希望记录其他两种类型的错误消息。如果您将其设置为零,则将禁用日志记录。

票数 2
EN

Stack Overflow用户

发布于 2016-07-16 23:27:35

在codeigniter 2中你不会遇到这个问题。

代码语言:javascript
复制
$config['log_threshold'] = array(1, 3);

在config/config.php中

代码语言:javascript
复制
log_message('info', 'The purpose of some variable is to provide some value.');

在您想要记录某些东西的地方,只会将该消息打印到日志文件中,而不会有多余的调试代码。

在codeIgniter 3中,许多系统核心文件都有

log_message('info',*);

喜欢

代码语言:javascript
复制
log_message('info', 'Config Class Initialized');
log_message('info', 'Controller Class Initialized');
log_message('info', 'Hooks Class Initialized');
............

这就是为什么当

代码语言:javascript
复制
$config['log_threshold'] = array(1, 3);

代码语言:javascript
复制
$config['log_threshold'] = array(3);

代码语言:javascript
复制
$config['log_threshold'] = 3;

则所有不需要的日志消息都会出现在日志文件中。

因此,解决此问题的一种方法是扩展所有核心文件并更改

代码语言:javascript
复制
log_message('info'

代码语言:javascript
复制
log_message('debug' 

或者创建一个新的阈值,比如

在config/config.php中

代码语言:javascript
复制
$config['log_threshold'] = array(1, 5);

在core/MY_Log.php中

代码语言:javascript
复制
protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4', 'USER_INFO' => '5');

完整的core/MY_Log.php文件

代码语言:javascript
复制
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);
    }

}
票数 4
EN

Stack Overflow用户

发布于 2013-07-25 03:54:16

我意识到这个问题已经很老了,但我自己也遇到了同样的问题,并且能够修改日志库来解决它。

修改Log.php中的write_log函数,如下所示:

代码语言:javascript
复制
  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;
    }

然后,您可以在配置文件中指定一个数组:

代码语言:javascript
复制
$config['log_threshold'] = array(3, 1);

然后,您的日志文件将如下所示:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4537033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档