瀏覽代碼

sql语句打印

林海涛 1 年之前
父節點
當前提交
190260512f
共有 1 個文件被更改,包括 25 次插入0 次删除
  1. 25 0
      app/common.php

+ 25 - 0
app/common.php

@@ -354,3 +354,28 @@ function http_request($url , $data = NULL)
 
 
     return $jsoninfo;
     return $jsoninfo;
 }
 }
+
+
+/**
+ * sql语句打印
+ * 需要打印sql时将record_sql()方法放到sql语句之前,或 config.database.trigger_sql设置为true
+ */
+function record_sql()
+{
+    if(!config("database.connections.mysql.trigger_sql")){
+        $config = config('database');
+        $config['connections']['mysql']['trigger_sql'] = true;
+        app()->config->set($config,'database');
+    }
+    \think\facade\Db::listen(function ($sql,$time,$connection) {
+        if(strpos($sql,'CONNECT') !== false){
+            return;
+        }
+        if(strpos($sql,'SHOW FULL') !== false){
+            return;
+        }
+        \think\facade\Log::debug( '打印sql: '.$sql. ' time:'.$time);
+    });
+}
+
+