有沒有辦法自定義CodeIgniter 2.1.0中由log_message()
生成的日誌的文件名?默認情況下,它的文件名是根據當前日期生成的。自定義codeigniter的日誌文件名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()
功能將作爲您的願望
隨着CI 2.1.3使工作使用此代碼。在庫文件夾中創建MY_Log.php。不是確切的什麼需要的OP,但每個人都會明白如何modyfy :)
<?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'not'.php' – 2017-01-15 09:27:13
不知道,可能CI開發者由於某種原因決定如此。 – 2017-01-15 10:01:11
對我來說似乎是一個潛在的後門,我將它改回日誌。 – 2017-02-08 11:57:56
- 1. 滾動的Log4Net日誌文件中的自定義文件名?
- 2. heroku上的自定義日誌文件
- 3. 自定義TFS生成日誌文件
- 4. WIX寫自定義日誌文件
- 5. 使用自定義名稱創建日誌文件
- 6. WiX Burn:爲MsiPackage創建自定義日誌文件名稱?
- 7. 使用Postharp自定義日誌文本
- 8. Neo4j 3+日誌自定義
- 9. logstash爲自定義日誌
- 10. 自定義日誌在Apache
- 11. Perl的 - 自定義日誌文件只顯示特定錯誤
- 12. 創建自定義事件日誌
- 13. 力定義的php.ini日誌文件
- 14. log4j的自定義日誌級別和屬性文件
- 15. 基於正則表達式的自定義Apache日誌文件
- 16. 具有正確權限的自定義日誌文件
- 17. 的Bash shell腳本 - 寫入自定義日誌文件
- 18. Codeigniter:從自定義庫中獲取自定義配置文件?
- 19. Wget自定義文件名
- 20. 自定義Log4j文件名
- 21. 自定義文件名
- 22. 要在日誌文件名
- 23. Rails的自定義錯誤日誌
- 24. 自定義神交模式的日誌
- 25. 谷歌日誌記錄中的自定義日誌級別
- 26. MobileFirst客戶端日誌寫日誌的自定義位置
- 27. NIFI:如何通過tailFail獲取自定義日誌文件?
- 28. 系統日誌與PHP和自定義文件
- 29. Python mod_wsgi寫入自定義日誌文件,而不是Apache error_log
- 30. 轉換自定義日誌文件導入TSV(shell腳本)
很好。感謝你! – 2012-04-02 06:09:51
獲取:致命錯誤:Class'CI_Controller'找不到D:\ projektai \ roachrun \ system \ core \ CodeIgniter.php在線233 – 2014-02-17 09:50:47
這是否仍然有效?我有字面上複製粘貼這個,只改變文件路徑,但日誌仍然使用默認文件名。也嘗試在開始時添加<?php,但這不會。 – 2017-08-19 01:21:07