添加日志功能

This commit is contained in:
Jaeger 2017-04-19 18:30:22 +08:00
parent 66c4ef8c4f
commit cbae16c6a4

View File

@ -3,6 +3,8 @@
namespace QL;
use phpQuery,Exception,ReflectionClass;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
/**
* QueryList
@ -55,10 +57,12 @@ class QueryList
{
public $data;
public $html;
private $page;
private $pqHtml;
private $outputEncoding = false;
private $inputEncoding = false;
private $htmlEncoding;
public static $logger = null;
public static $instances;
public function __construct() {
@ -96,6 +100,20 @@ class QueryList
return $extension->run($args);
}
/**
* 日志设置
* @param $handler
*/
public static function setLog($handler)
{
if(is_string($handler))
{
$handler = new StreamHandler($handler,Logger::INFO);
}
self::$logger = new Logger('QueryList');
self::$logger->pushHandler($handler);
}
/**
* 获取任意实例
* @return mixed
@ -157,13 +175,15 @@ class QueryList
private function _query($page,array $rules, $range, $outputEncoding, $inputEncoding,$removeHead)
{
$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;
$inputEncoding && $this->inputEncoding = $inputEncoding;
$removeHead && $this->html = $this->_removeHead($this->html);
$this->pqHtml = '';
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);
@ -171,6 +191,7 @@ class QueryList
$this->regArr = $rules;
$this->regRange = $range;
$this->_getList();
$this->_log();
return $this;
}
@ -398,6 +419,26 @@ class QueryList
}
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
));
}
}
}
/*