Merge branch 'feature/log' into develop

This commit is contained in:
Jaeger 2017-04-20 13:53:32 +08:00
commit 700d56db49

View File

@ -3,6 +3,8 @@
namespace QL; namespace QL;
use phpQuery,Exception,ReflectionClass; use phpQuery,Exception,ReflectionClass;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
/** /**
* QueryList * QueryList
@ -55,10 +57,12 @@ class QueryList
{ {
public $data; public $data;
public $html; public $html;
private $page;
private $pqHtml; private $pqHtml;
private $outputEncoding = false; private $outputEncoding = false;
private $inputEncoding = false; private $inputEncoding = false;
private $htmlEncoding; private $htmlEncoding;
public static $logger = null;
public static $instances; public static $instances;
public function __construct() { public function __construct() {
@ -96,6 +100,27 @@ class QueryList
return $extension->run($args); return $extension->run($args);
} }
/**
* 日志设置
* @param $handler
*/
public static function setLog($handler)
{
if(class_exists(Logger::class))
{
if(is_string($handler))
{
$handler = new StreamHandler($handler,Logger::INFO);
}
self::$logger = new Logger('QueryList');
self::$logger->pushHandler($handler);
}else{
throw new Exception("You need to install the package [monolog/monolog]");
}
}
/** /**
* 获取任意实例 * 获取任意实例
* @return mixed * @return mixed
@ -157,13 +182,15 @@ class QueryList
private function _query($page,array $rules, $range, $outputEncoding, $inputEncoding,$removeHead) private function _query($page,array $rules, $range, $outputEncoding, $inputEncoding,$removeHead)
{ {
$this->data = array(); $this->data = array();
$this->html = $this->_isURL($page)?$this->_request($page):$page; $this->page = $page;
$this->html = $this->_isURL($this->page)?$this->_request($this->page):$this->page;
$outputEncoding && $this->outputEncoding = $outputEncoding; $outputEncoding && $this->outputEncoding = $outputEncoding;
$inputEncoding && $this->inputEncoding = $inputEncoding; $inputEncoding && $this->inputEncoding = $inputEncoding;
$removeHead && $this->html = $this->_removeHead($this->html); $removeHead && $this->html = $this->_removeHead($this->html);
$this->pqHtml = ''; $this->pqHtml = '';
if(empty($this->html)){ if(empty($this->html)){
trigger_error("The received content is empty!",E_USER_NOTICE); $this->_log('The received content is empty!','error');
trigger_error('The received content is empty!',E_USER_NOTICE);
} }
//获取编码格式 //获取编码格式
$this->htmlEncoding = $this->inputEncoding?$this->inputEncoding:$this->_getEncode($this->html); $this->htmlEncoding = $this->inputEncoding?$this->inputEncoding:$this->_getEncode($this->html);
@ -171,6 +198,7 @@ class QueryList
$this->regArr = $rules; $this->regArr = $rules;
$this->regRange = $range; $this->regRange = $range;
$this->_getList(); $this->_getList();
$this->_log();
return $this; return $this;
} }
@ -398,6 +426,26 @@ class QueryList
} }
return $html; return $html;
} }
/**
* 打印日志
* @param string $message
* @param string $level
*/
private function _log($message = '',$level = 'info')
{
if(!is_null(self::$logger))
{
$url = $this->_isURL($this->page)?$this->page:'[html]';
$count = count($this->data);
$level = empty($level)?($count?'info':'warning'):$level;
$message = empty($message)?($count?'Get data successfully':'Get data failed'):$message;
self::$logger->$level($message,array(
'page' => $url,
'count' => $count
));
}
}
} }
/* /*