|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class LogService |
|
12
|
|
|
* |
|
13
|
|
|
* @package App\Services |
|
14
|
|
|
*/ |
|
15
|
|
|
class LogService |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Get full trace of an throwable. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Throwable $throwable |
|
21
|
|
|
* @param Request|null $request |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function getThrowableTraceAsString(Throwable $throwable, Request $request = null) |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var User|null $user */ |
|
28
|
|
|
$user = Auth::user(); |
|
29
|
|
|
|
|
30
|
|
|
$errorString = '#00 Exception: ' . $throwable->getCode() . ' --- ' . $throwable->getMessage() . "\n"; |
|
31
|
|
|
$errorString .= '#01 User: ' . ($user ? $user->id . ' --- ' . $user->name : '') . "\n"; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
if ($request) { |
|
34
|
|
|
$errorString .= '#02 Request: ' . json_encode($request->all(), JSON_UNESCAPED_UNICODE) . "\n"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$count = 0; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($throwable->getTrace() as $frame) { |
|
40
|
|
|
$args = ''; |
|
41
|
|
|
|
|
42
|
|
|
if (isset($frame['args'])) { |
|
43
|
|
|
$args = self::getFrameArgs($frame['args']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$currentFile = isset($frame['file']) ? $frame['file'] : '[internal function]'; |
|
47
|
|
|
$currentLine = isset($frame['line']) ? $frame['line'] : ''; |
|
48
|
|
|
|
|
49
|
|
|
$errorString .= sprintf("#%s %s(%s): %s(%s)\n", |
|
50
|
|
|
$count, |
|
51
|
|
|
$currentFile, |
|
52
|
|
|
$currentLine, |
|
53
|
|
|
$frame['function'], |
|
54
|
|
|
$args); |
|
55
|
|
|
$count++; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $errorString . "\n"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get exception args |
|
63
|
|
|
* |
|
64
|
|
|
* @param $frameArgs |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
private static function getFrameArgs($frameArgs) |
|
69
|
|
|
{ |
|
70
|
|
|
$args = array(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($frameArgs as $arg) { |
|
73
|
|
|
if (is_string($arg)) { |
|
74
|
|
|
$args[] = '\'' . $arg . '\''; |
|
75
|
|
|
} elseif (is_array($arg)) { |
|
76
|
|
|
$args[] = 'Array'; |
|
77
|
|
|
} elseif (is_null($arg)) { |
|
78
|
|
|
$args[] = 'NULL'; |
|
79
|
|
|
} elseif (is_bool($arg)) { |
|
80
|
|
|
$args[] = ($arg) ? 'true' : 'false'; |
|
81
|
|
|
} elseif (is_object($arg)) { |
|
82
|
|
|
$args[] = get_class($arg); |
|
83
|
|
|
} elseif (is_resource($arg)) { |
|
84
|
|
|
$args[] = get_resource_type($arg); |
|
85
|
|
|
} else { |
|
86
|
|
|
$args[] = $arg; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return join(', ', $args); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|