thinkphp3.1 修改日志逻辑,定位每个sql的代码执行行数

thinkphp3.1 修改日志

修改内容如下,这样的话就可以记录日志的时候定位到具体的代码行数,快速排bug

Index: Core/Lib/Core/Db.class.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Core/Lib/Core/Db.class.php b/Core/Lib/Core/Db.class.php
--- a/Core/Lib/Core/Db.class.php    (revision 6dbb57381127540a2021f1037fec772dd77379de)
+++ b/Core/Lib/Core/Db.class.php    (revision f96daf8918efed7e5543b173f15f2c4cef76297c)
@@ -249,9 +249,13 @@
         $this->modelSql[$this->model]   =  $this->queryStr;
         $this->model  =   '_think_';
         // 记录操作结束时间
-        if (C('DB_SQL_LOG')) {
+        if (C('DB_SQL_LOG') && strpos($this->queryStr, 'SHOW COLUMNS') === false) {
             G('queryEndTime');
-            trace($this->queryStr.' [ RunTime:'.G('queryStartTime','queryEndTime',6).'s ]','','SQL');
+            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+            file_put_contents('sql.log',json_encode($trace,320));
+            $file = isset($trace[3]['file']) ? basename($trace[3]['file']) : 'unknown';
+            $line = isset($trace[3]['line']) ? $trace[3]['line'] : 'unknown';
+            trace($this->queryStr.";[$file:$line]".' [ RunTime:'.G('queryStartTime','queryEndTime',6).'s ]','','SQL');
         }
     }
 
Index: Core/Lib/Core/Log.class.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Core/Lib/Core/Log.class.php b/Core/Lib/Core/Log.class.php
--- a/Core/Lib/Core/Log.class.php   (revision 6dbb57381127540a2021f1037fec772dd77379de)
+++ b/Core/Lib/Core/Log.class.php   (revision f96daf8918efed7e5543b173f15f2c4cef76297c)
@@ -135,7 +135,11 @@
             $destination   =   $destination?$destination:C('LOG_DEST');
             $extra   =  $extra?$extra:C('LOG_EXTRA');
         }
-        error_log("{$now} {$level}: {$message}rn", $type,$destination,$extra );
+        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
+        $file = isset($trace[2]['file']) ? basename($trace[2]['file']) : 'unknown';
+        $line = isset($trace[2]['line']) ? $trace[2]['line'] : 'unknown';
+
+        error_log("{$now} [$file:$line] {$level}: {$message}rn", $type,$destination,$extra );
         clearstatcache();
         //增加skywalking日志
         if ($message != "" && function_exists("skywalking_log")) {

改完之后日志就如下面所示,剋清楚的看到具体执行sql的代码行数:

所有用户都可以去薅羊毛,192元充值200元话费!先到先得!导航栏话费充值,正规可靠,快充慢充自由选择。
欧阳逸资源站 » thinkphp3.1 修改日志逻辑,定位每个sql的代码执行行数

发表评论