Browse Source

exception日志修复

LD 5 years ago
parent
commit
f16a59be65
1 changed files with 33 additions and 6 deletions
  1. 33 6
      app/Exceptions/Exception.php

+ 33 - 6
app/Exceptions/Exception.php

@@ -4,14 +4,41 @@
 namespace App\Exceptions;
 
 
+use App\Services\LogService;
+use Illuminate\Support\Facades\Auth;
 use Throwable;
 
 class Exception extends \Exception
 {
-        public $type;
-        public function __construct($message = "",$type='error', $code = 0, Throwable $previous = null)
-        {
-            parent::__construct($message, $code, $previous);
-            $this->type=$type;
-        }
+    public $type;
+    public function __construct($message = "",$type='error', $code = 0, Throwable $previous = null)
+    {
+        parent::__construct($message, $code, $previous);
+        $this->type=$type;
+        $this->logging();
+    }
+    public function logging()
+    {
+        $exception=$this;
+        list(
+            $className,
+            $functionName,
+            $tracesAll
+            )           =(function()use($exception){
+            $traces=method_exists($exception,'getTraceAsString')
+                ?($exception->getTraceAsString()??'')
+                :'';
+            if(!$traces)return '';
+            preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
+            return [$result[1]??'',$result[3]??'',$traces];
+        })();
+        LogService::log(
+            $className,
+            $functionName,
+            ($exception->getMessage()??'')
+            .'调用堆栈:'.$tracesAll,
+            Auth::id()??'',
+            $this->type
+        );
+    }
 }