CodeIgniter源代码阅读(四)/system/library/Log.php

  1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2 /**
  3  * CodeIgniter
  4  *
  5  * An open source application development framework for PHP 5.1.6 or newer
  6  *
  7  * @package        CodeIgniter
  8  * @author        ExpressionEngine Dev Team
  9  * @copyright    Copyright (c) 2008 - 2011, EllisLab, Inc.
 10  * @license        http://codeigniter.com/user_guide/license.html
 11  * @link        http://codeigniter.com
 12  * @since        Version 1.0
 13  * @filesource
 14  */
 15 
 16 // ------------------------------------------------------------------------
 17 
 18 /**
 19  * Logging Class
 20  *
 21  * @package        CodeIgniter
 22  * @subpackage    Libraries
 23  * @category    Logging
 24  * @author        ExpressionEngine Dev Team
 25  * @link        http://codeigniter.com/user_guide/general/errors.html
 26  */
 27 class CI_Log {
 28 
 29     protected $_log_path;//日志路径
 30     protected $_threshold    = 1;
 31     protected $_date_fmt    = 'Y-m-d H:i:s';//日期格式
 32     protected $_enabled    = TRUE;
 33     protected $_levels    = array('ERROR' => '1', 'DEBUG' => '2',  'INFO' => '3', 'ALL' => '4');
 34 
 35     /**
 36      * Constructor
 37      */
 38     public function __construct()
 39     {
 40         $config =& get_config();
 41         $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';//日志路径  如果没有设置默认为 application/logs/
 42 
 43         if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))//路径是否合法可写
 44         {
 45             $this->_enabled = FALSE;
 46         }
 47 
 48         if (is_numeric($config['log_threshold']))//记录类型
 49         {
 50             $this->_threshold = $config['log_threshold'];
 51         }
 52 
 53         if ($config['log_date_format'] != '')//日期格式
 54         {
 55             $this->_date_fmt = $config['log_date_format'];
 56         }
 57     }
 58 
 59     // --------------------------------------------------------------------
 60 
 61     /**
 62      * Write Log File 写错误入日志
 63      *
 64      * Generally this function will be called using the global log_message() function
 65      *
 66      * @param    string    the error level
 67      * @param    string    the error message
 68      * @param    bool    whether the error is a native PHP error
 69      * @return    bool
 70      */
 71     public function write_log($level = 'error', $msg$php_error = FALSE)//写日志
 72     {
 73         if ($this->_enabled === FALSE)
 74         {
 75             return FALSE;
 76         }
 77 
 78         $level = strtoupper($level);
 79 
 80         if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
 81         {
 82             return FALSE;
 83         }
 84 
 85         $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
 86         $message  = '';
 87 
 88         if ( ! file_exists($filepath))
 89         {
 90             $message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
 91         }
 92 
 93         if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
 94         {
 95             return FALSE;
 96         }
 97 
 98         $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
 99 
100         flock($fp, LOCK_EX);//轻便的咨询文件锁定 LOCK_EX:要取得独占锁定(写入的程序
101         fwrite($fp$message);
102         flock($fp, LOCK_UN);//要释放锁定(无论共享或独占)
103         fclose($fp);
104 
105         @chmod($filepath, FILE_WRITE_MODE);//修改文件的权限
106         return TRUE;
107     }
108 
109 }
110 // END Log Class
111 
112 /* End of file Log.php */
113 /* Location: ./system/libraries/Log.php */
posted @ 2012-11-20 10:34  Linux、Mongo、Php、Shell、Python、C  阅读(360)  评论(0编辑  收藏  举报