From 64c976e11dd73ca2faa377922ab1fecaf54c195b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 2 Apr 2013 17:46:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BApp=E3=80=81Error=E5=92=8CVie?= =?UTF-8?q?w=E7=B1=BB=20=E6=94=B9=E8=BF=9BLog=E7=B1=BB=20=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E9=A9=B1=E5=8A=A8=E7=B1=BB=E7=9A=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Think/App.php | 7 +++- Think/Error.php | 2 +- Think/Log.php | 78 ++++++++++++++++++++++++++++++++------- Think/Log/Driver/File.php | 76 ++++---------------------------------- Think/View.php | 2 +- 5 files changed, 79 insertions(+), 86 deletions(-) diff --git a/Think/App.php b/Think/App.php index e60614c9..de3729ba 100644 --- a/Think/App.php +++ b/Think/App.php @@ -133,7 +133,7 @@ class App { $var_c = $config['var_controller']; $var_a = $config['var_action']; $var_p = $config['var_pathinfo']; - if(!empty($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数 + if(isset($_GET[$var_p])) { // 判断URL里面是否有兼容模式参数 $_SERVER['PATH_INFO'] = $_GET[$var_p]; unset($_GET[$var_p]); }elseif(IS_CLI){ // CLI模式下 index.php module/controller/action/params/... @@ -185,7 +185,7 @@ class App { // 监听path_info Tag::listen('path_info'); // 分析PATHINFO信息 - if(empty($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) { + if(!isset($_SERVER['PATH_INFO']) && $_SERVER['SCRIPT_NAME'] != $_SERVER['PHP_SELF']) { $types = explode(',',$config['pathinfo_fetch']); foreach ($types as $type){ if(0===strpos($type,':')) {// 支持函数判断 @@ -199,6 +199,9 @@ class App { } } // 定位模块 + if(empty($_SERVER['PATH_INFO'])) { + $_SERVER['PATH_INFO'] = ''; + } $part = pathinfo($_SERVER['PATH_INFO']); define('__EXT__', isset($part['extension'])?strtolower($part['extension']):''); $_SERVER['PATH_INFO'] = trim(preg_replace('/\.('.trim($config['url_html_suffix'],'.').')$/i', '',$_SERVER['PATH_INFO']),'/'); diff --git a/Think/Error.php b/Think/Error.php index 40e03c08..da9807ec 100644 --- a/Think/Error.php +++ b/Think/Error.php @@ -43,7 +43,7 @@ class Error { case E_COMPILE_ERROR: case E_USER_ERROR: $errorStr = "[$errno] $errstr ".$errfile." 第 $errline 行."; - Log::write($errorStr,'ERROR'); + Log::record($errorStr,'ERROR'); self::halt($errorStr); break; case E_STRICT: diff --git a/Think/Log.php b/Think/Log.php index 27770ccd..17506f35 100644 --- a/Think/Log.php +++ b/Think/Log.php @@ -12,22 +12,74 @@ namespace Think; class Log { - static protected $handler = null; + // 日志信息 + static protected $log = []; + static protected $level = ['ERR','NOTIC','DEBUG','SQL','INFO']; + static protected $storage = null; // 日志初始化 static public function init($config=[]){ - if(!empty($config['type'])) { // 读取log驱动 - $class = 'Think\\Log\\Driver\\'. ucwords(strtolower($config['type'])); - // 检查驱动类 - unset($config['type']); - self::$handler = new $class($config); - return self::$handler; - } + $type = isset($config['type'])?$config['type']:'File'; + $class = 'Think\\Log\\Driver\\'. ucwords($type); + unset($config['type']); + self::$storage = new $class($config); + } + + /** + * 记录日志 并且会过滤未经设置的级别 + * @access public + * @param string $message 日志信息 + * @param string $level 日志级别 + * @param boolean $record 是否强制记录 + * @return void + */ + static public function record($message,$level='INFO') { + self::$log[$level][] = "{$level}: {$message}"; + } + + /** + * 获取内存中的日志信息 + * @access public + * @param string $level 日志级别 + * @return array + */ + static public function getLog($level=''){ + return $level?self::$log[$level]:self::$log; + } + + /** + * 日志保存 + * @access public + * @param string $destination 写入目标 + * @param string $level 保存的日志级别 + * @return void + */ + static public function save($destination='',$level='') { + $log = $level?self::$log[$level]:self::$log; + if(empty($log)) return ; + $message = ''; + if($level) { + $message .= implode("\r\n",$log); + self::$log[$level] = []; + }else{ + foreach($log as $info){ + $message .= implode("\r\n",$info)."\r\n"; + } + self::$log = []; + } + self::$storage->write($message,$destination); + } + + /** + * 日志直接写入 + * @access public + * @param string $log 日志信息 + * @param string $level 日志级别 + * @param string $destination 写入目标 + * @return void + */ + static public function write($log,$level,$destination='') { + self::$storage->write("{$level}: {$log}",$destination); } - - // 调用驱动类的方法 - static public function __callStatic($method, $params){ - return call_user_func_array(array(self::$handler, $method), $params); - } } \ No newline at end of file diff --git a/Think/Log/Driver/File.php b/Think/Log/Driver/File.php index 9adb020d..7f2d139f 100644 --- a/Think/Log/Driver/File.php +++ b/Think/Log/Driver/File.php @@ -12,80 +12,18 @@ namespace Think\Log\Driver; class File { - - // 日志记录方式 - const SYSTEM = 0; - const MAIL = 1; - const FILE = 3; - const SAPI = 4; - // 日志信息 - protected $log = []; - protected $config = array( - 'log_time_format' => '[ c ]', + protected $config = [ + 'log_time_format' => ' c ', 'log_file_size' => 2097152, - 'log_allow_level' => array('ERR','NOTIC','DEBUG','SQL','INFO'), - ); + 'log_path' => '', + ]; + // 实例化并传入参数 public function __construct($config=[]){ $this->config = array_merge($this->config,$config); } - /** - * 记录日志 并且会过滤未经设置的级别 - * @access public - * @param string $message 日志信息 - * @param string $level 日志级别 - * @param boolean $record 是否强制记录 - * @return void - */ - public function record($message,$level='INFO',$record=false) { - if($record || false !== array_search($level,$this->config['log_allow_level'])) { - $this->log[$level][] = "{$level}: {$message}\r\n"; - } - } - - /** - * 获取内存中的日志信息 - * @access public - * @param string $level 日志级别 - * @return array - */ - public function getLog($level=''){ - return $level?$this->log[$level]:$this->log; - } - - /** - * 日志保存 - * @access public - * @param string $destination 写入目标 - * @param string $level 保存的日志级别 - * @return void - */ - public function save($destination='',$level='') { - $log = $level?$this->log[$level]:$this->log; - if(empty($log)) return ; - if(empty($destination)) - $destination = $this->config['log_path'].date('y_m_d').'.log'; - - //检测日志文件大小,超过配置大小则备份日志文件重新生成 - if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) ) - rename($destination,dirname($destination).'/'.time().'-'.basename($destination)); - - $message = date($this->config['log_time_format']).' '.$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n"; - if($level) { - $message .= implode('',$log)."\r\n"; - $this->log[$level] = []; - }else{ - foreach($log as $info){ - $message .= implode('',$info)."\r\n"; - } - $this->log = []; - } - error_log($message, 3,$destination); - //clearstatcache(); - } - /** * 日志直接写入 * @access public @@ -94,13 +32,13 @@ class File { * @param string $destination 写入目标 * @return void */ - public function write($log,$level,$destination='') { + public function write($log,$destination='') { $now = date($this->config['log_time_format']); if(empty($destination)) $destination = $this->config['log_path'].date('y_m_d').'.log'; //检测日志文件大小,超过配置大小则备份日志文件重新生成 if(is_file($destination) && floor($this->config['log_file_size']) <= filesize($destination) ) rename($destination,dirname($destination).'/'.time().'-'.basename($destination)); - error_log("{$now} {$level}: {$log}\r\n", 3,$destination); + error_log("[{$now}] ".$_SERVER['REMOTE_ADDR'].' '.$_SERVER['REQUEST_URI']."\r\n{$log}\r\n", 3,$destination); } } \ No newline at end of file diff --git a/Think/View.php b/Think/View.php index a0fbc3f2..121bdb69 100644 --- a/Think/View.php +++ b/Think/View.php @@ -63,7 +63,7 @@ class View { * @param boolean $return 是否返回 * @return mixed */ - public function display($template='',$vars=[],$cacheId,$return=false) { + public function display($template='',$vars=[],$cacheId='',$return=false) { Tag::listen('view_begin',$template); // 解析并获取模板内容 $content = $this->fetch($template,$vars,$cacheId);