From cbae16c6a41e055852a978da7f718592246f63eb Mon Sep 17 00:00:00 2001
From: Jaeger <JaegerCode@gmail.com>
Date: Wed, 19 Apr 2017 18:30:22 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?=
 =?UTF-8?q?=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 QueryList.php | 45 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/QueryList.php b/QueryList.php
index 53d3176..adc0761 100644
--- a/QueryList.php
+++ b/QueryList.php
@@ -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
+            ));
+        }
+    }
 }
 
 /*

From 1691ddf3ee2ced4664be45521deeb48d41d9de84 Mon Sep 17 00:00:00 2001
From: Jaeger <JaegerCode@gmail.com>
Date: Thu, 20 Apr 2017 13:53:03 +0800
Subject: [PATCH 2/2] log ok

---
 QueryList.php | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/QueryList.php b/QueryList.php
index adc0761..0ba8309 100644
--- a/QueryList.php
+++ b/QueryList.php
@@ -106,12 +106,19 @@ class QueryList
      */
     public static function setLog($handler)
     {
-        if(is_string($handler))
-        {
-            $handler = new StreamHandler($handler,Logger::INFO);
-        }
-        self::$logger = new Logger('QueryList');
-        self::$logger->pushHandler($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]");
+    		
+    	}
+        
     }
 
     /**