<?php


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;
        $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()??'')
            .'调用堆栈e：'.$tracesAll,
            Auth::id()??'',
            $this->type
        );
    }
}
