简体   繁体   English

codeigniter更改Common.php函数

[英]codeigniter alter Common.php functions

I'm trying to customize the logging functionality of codeigniter. 我正在尝试自定义codeigniter的日志记录功能。 I found this forum thread which describes exactly what I need: http://codeigniter.com/forums/viewthread/139997/ But I want to do that without altering core files/classes. 我找到了这个论坛线程,它准确地描述了我的需求: http : //codeigniter.com/forums/viewthread/139997/但我想做到这一点而又不改变核心文件/类。

I"m trying to alter the log_message function. I've already extended the CI_Log class and that is working well, but now I'm trying to alter log_message() which resides in system/core/Common.php. It isn't actually a class so I can't extend it, it just appears to be a collection of useful functions. I tried redeclaring log_message() and placing it in application/core/Common.php, but that doesn't appear to work. Any ideas how I can go about this? 我正在尝试更改log_message函数。我已经扩展了CI_Log类并且运行良好,但是现在我正在尝试更改位于system / core / Common.php中的log_message()。实际上是一个类,因此我无法对其进行扩展,它似乎只是有用的功能的集合。我尝试重新声明log_message()并将其放在application / core / Common.php中,但这似乎不起作用。想法我该怎么做?

I believe that the function you actually want to alter is write_log() , correct me if I'm wrong. 我相信您实际上想要更改的功能是write_log() ,如果我错了,请纠正我。 You can do this by extending the function within application\\libraries\\MY_Log.php . 您可以通过扩展application\\libraries\\MY_Log.php的函数来完成此application\\libraries\\MY_Log.php

I have the following script in my MY_Log.php which emails me any time an error is thrown: 我的MY_Log.php中包含以下脚本,该脚本会在引发错误时向我发送电子邮件:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* Extends the logging class to send an email when an error is triggered */

class MY_Log extends CI_Log {

    function __construct()
    {
        parent::__construct();
    }

    function write_log($level = 'error', $msg, $php_error = FALSE, $additional, $additional2)

    {   
        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;
        }

        $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;
    }

}

If you're not looking to extend the write function, you will need to extend one of the other log functions. 如果您不想扩展写入功能,则需要扩展其他日志功能之一。

A helper function could work here. 辅助功能可以在这里工作。 You'd have to use it instead of log_message(). 您必须使用它而不是log_message()。

Here's what I did: 这是我所做的:

in application/helpers I created 'new_log_helper.php' (helpers have to end in _helper.php) I wanted this to add log entries into the database and to track by process (that way I know where to look in the code). 在应用程序/帮助程序中,我创建了“ new_log_helper.php”(帮助程序必须以_helper.php结尾),我希望此操作可以将日志条目添加到数据库中并按进程进行跟踪(这样我就知道在代码中查找的位置)。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Custom Logging Interface
 *
 * Add log entries to a log table on the db and track process name
 *
 * @access  public
 * @return  void
 */
if ( ! function_exists('new_log')) {
    function new_log($level = 'error', $message, $process, $php_error = FALSE)
    {
        if (config_item('log_threshold') == 0) {
            return;
        }

        $CI =& get_instance();
        $CI->load->model('new_log','logger', TRUE);
        $CI->logger->add_event($process, $level, $message);

        log_message($level, $process.': '.$message, $php_error);
    }
}

Then when you need it use it like this: 然后在需要时使用它,如下所示:

$this->load->helper('new_log');
...
new_log('debug','Something happened','Stack overflow answer');

Anyway I know your issue was years ago but I was looking so maybe someone else is too. 无论如何,我知道您的问题是几年前的,但我一直在寻找,所以也许也有人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM