【问题标题】:customize log filename of codeigniter log_message()自定义 codeigniter log_message() 的日志文件名
【发布时间】:2012-04-15 19:27:43
【问题描述】:

CodeIgniter 2.1.0 中是否可以自定义log_message() 生成的日志文件名?默认情况下,它的文件名是根据当前日期生成的。

【问题讨论】:

    标签: php codeigniter logging


    【解决方案1】:

    log_message()函数是常用的系统函数。它使用Log::write_log() 方法记录错误。它不好破解核心文件。所以你可以扩展日志库并覆盖write_log()函数。

    如果您还没有扩展 Log 课程。创建文件application/libraries/MY_Log.php

    class MY_Log extends CI_Log  {
    
        function MY_Log ()
        {
            parent::__construct();
    
            $this->ci =& get_instance();
        }
    
        public function write_log() { //here overriding
            if ($this->_enabled === FALSE)
            {
            return FALSE;
            }
    
            $level = strtoupper($level);
    
            if ( ! isset($this->_levels[$level]) OR
            ($this->_levels[$level] > $this->_threshold))
            {
            return FALSE;
            }
    
            /* HERE YOUR LOG FILENAME YOU CAN CHANGE ITS NAME */
            $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, FOPEN_WRITE_CREATE))
            {
            return FALSE;
            }
    
            $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' ';
            $message .= 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;
        }
    }
    

    log_message() 功能将按您的意愿工作

    【讨论】:

    • Gets:Fatal error: Class 'CI_Controller' not found in D:\projektai\roachrun\system\core\CodeIgniter.php on line 233
    • 这还能用吗?我已经复制粘贴了这个,只更改了文件路径,但日志仍然使用默认文件名。还尝试在开始时添加
    【解决方案2】:

    借助 CI 2.1.3,它可以使用此代码工作。在库文件夹中创建了 MY_Log.php。不确切需要什么 OP,但每个人都会明白如何修改 :)

    <?php 
    /**
     * CodeIgniter
     *
     * An open source application development framework for PHP 4.3.2 or newer
     *
     * @package        CodeIgniter
     * @author        ExpressionEngine Dev Team
     * @copyright    Copyright (c) 2006, EllisLab, Inc.
     * @license        http://codeigniter.com/user_guide/license.html
     * @link        http://codeigniter.com
     * @since        Version 1.0
     * @filesource
     */
    // ------------------------------------------------------------------------
    /**
     * MY_Logging Class
     *
     * This library assumes that you have a config item called
     * $config['show_in_log'] = array();
     * you can then create any error level you would like, using the following format
     * $config['show_in_log']= array('DEBUG','ERROR','INFO','SPECIAL','MY_ERROR_GROUP','ETC_GROUP'); 
     * Setting the array to empty will log all error messages. 
     * Deleting this config item entirely will default to the standard
     * error loggin threshold config item. 
     *
     * @package        CodeIgniter
     * @subpackage    Libraries
     * @category    Logging
     * @author        ExpressionEngine Dev Team. Mod by Chris Newton
     */
    class MY_Log extends CI_Log {
        /**
         * Constructor
         */
        public function __construct()
        {
            $config =& get_config();
    
            $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
    
            if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
            {
                $this->_enabled = FALSE;
            }
    
            if (is_numeric($config['log_threshold']))
            {
                $this->_threshold = $config['log_threshold'];
            }
    
            if ($config['log_date_format'] != '')
            {
                $this->_date_fmt = $config['log_date_format'];
            }
        }
    
        private function isCommandLineInterface()
        {
            return (php_sapi_name() === 'cli');
        }
    
        // --------------------------------------------------------------------
        /**
         * Write Log File
         *
         * Generally this function will be called using the global log_message() function
         *
         * @access    public
         * @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))
            {
                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;
            }
    
            if ($this->isCommandLineInterface()) {
                $message .= 'CMD ';
            }
    
            $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;
        }
    
    }
    

    【讨论】:

    • 我想知道为什么有php的扩展而不是log? '.log' 不是 '.php'
    • 不知道,可能 CI 开发人员出于某种原因决定这样做。
    • 对我来说似乎是一个潜在的后门,我将其改回日志。
    【解决方案3】:

    @safarov 的回答很棒,但是在具体代码方面有点过时了,下面是如何制作自己的:

    • 按照@safarov 的描述创建您的 MY_Log 类,但不要覆盖该方法。
    • 找到包含 CI_Log 类的文件,在 CI 3.1.10 中它被称为 Log.php,如果你使用 composer,你可能会在 /vendor/codeigniter/framework/system/core/Log.php 找到它
    • 在该文件中,找到 write_log 函数并将整个内容复制到您的新类中。
    • 编辑该函数以根据需要命名文件。
    • 更新 CI 时别忘了更新这个函数!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多