自定义codeigniter log_message()的日志文件名


customize log filename of codeigniter log_message()

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

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()功能将按您的意愿工作

@safarov的答案很好,但在特定代码方面有点过时,下面是如何制作自己的:

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

使用CI 2.1.3使其能够使用此代码。在libraries文件夹中创建了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;
    }
}