mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-02 13:31:38 +08:00
改进App、Error和View类
改进Log类 简化驱动类的定义
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user