Skip to content

Commit a0f555e

Browse files
committed
improve and move some functions to helper
1 parent 669b9c1 commit a0f555e

File tree

2 files changed

+46
-58
lines changed

2 files changed

+46
-58
lines changed

src/DBTransactionRetryHelper.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ protected static function buildLogContext(QueryException $e, int $attempt, int $
139139
'Exception' => get_class($e),
140140
'message' => $e->getMessage(),
141141
'sql' => $sql,
142-
'bindings' => static::stringifyBindings($bindings),
142+
'bindings' => stringifyBindings($bindings),
143143
'errorInfo' => $e->errorInfo,
144144
'connection' => $connectionName,
145-
'trace' => static::safeTrace(),
145+
'trace' => getDebugBacktraceArray(),
146146
]);
147147
}
148148

@@ -158,49 +158,4 @@ protected static function backoffDelay(int $baseDelay, int $attempt): int
158158
$max = $delay + $jitter;
159159
return random_int($min, $max);
160160
}
161-
162-
protected static function stringifyBindings(array $bindings): array
163-
{
164-
return array_map(function ($b) {
165-
if ($b instanceof \DateTimeInterface) {
166-
return $b->format('Y-m-d H:i:s.u');
167-
}
168-
if (is_object($b)) {
169-
return '[object ' . get_class($b) . ']';
170-
}
171-
if (is_resource($b)) {
172-
return '[resource]';
173-
}
174-
if (is_string($b)) {
175-
// Trim very long strings to avoid log bloat
176-
return mb_strlen($b) > 500 ? (mb_substr($b, 0, 500) . '…[+trimmed]') : $b;
177-
}
178-
if (is_array($b)) {
179-
// Compact arrays
180-
$json = @json_encode($b, JSON_UNESCAPED_UNICODE);
181-
182-
return $json !== false
183-
? (mb_strlen($json) > 500 ? (mb_substr($json, 0, 500) . '…[+trimmed]') : $json)
184-
: '[array]';
185-
}
186-
187-
return $b;
188-
}, $bindings);
189-
}
190-
191-
protected static function safeTrace(): array
192-
{
193-
try {
194-
return collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 15))
195-
->map(fn($f) => [
196-
'file' => $f['file'] ?? null,
197-
'line' => $f['line'] ?? null,
198-
'function' => $f['function'] ?? null,
199-
'class' => $f['class'] ?? null,
200-
'type' => $f['type'] ?? null,
201-
])->all();
202-
} catch (Throwable) {
203-
return [];
204-
}
205-
}
206161
}

src/Helper.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,52 @@
22

33
use Illuminate\Support\Facades\Log;
44

5-
if (!function_exists('getDebugBacktraceArray')) {
6-
function getDebugBacktraceArray(): array
5+
if (! function_exists('getDebugBacktraceArray')) {
6+
function getDebugBacktraceArray(int $option = DEBUG_BACKTRACE_IGNORE_ARGS, int $limit = 15): array
77
{
8-
$steps = [];
9-
foreach (debug_backtrace() as $step) {
10-
$steps[] = [
11-
'file' => $step['file'] ?? '',
12-
'class' => $step['class'] ?? '',
13-
'function' => $step['function'] ?? '',
14-
'line' => $step['line'] ?? '',
15-
];
8+
try {
9+
return collect(debug_backtrace($option, $limit))
10+
->map(fn ($f) => [
11+
'file' => $f['file'] ?? null,
12+
'line' => $f['line'] ?? null,
13+
'function' => $f['function'] ?? null,
14+
'class' => $f['class'] ?? null,
15+
'type' => $f['type'] ?? null,
16+
])->all();
17+
} catch (Throwable) {
18+
return [];
1619
}
17-
return $steps;
20+
}
21+
}
22+
23+
if (! function_exists('stringifyBindings')) {
24+
function stringifyBindings(array $bindings): array
25+
{
26+
return array_map(function ($b) {
27+
if ($b instanceof \DateTimeInterface) {
28+
return $b->format('Y-m-d H:i:s.u');
29+
}
30+
if (is_object($b)) {
31+
return '[object '.get_class($b).']';
32+
}
33+
if (is_resource($b)) {
34+
return '[resource]';
35+
}
36+
if (is_string($b)) {
37+
// Trim very long strings to avoid log bloat
38+
return mb_strlen($b) > 500 ? (mb_substr($b, 0, 500).'…[+trimmed]') : $b;
39+
}
40+
if (is_array($b)) {
41+
// Compact arrays
42+
$json = @json_encode($b, JSON_UNESCAPED_UNICODE);
43+
44+
return $json !== false
45+
? (mb_strlen($json) > 500 ? (mb_substr($json, 0, 500).'…[+trimmed]') : $json)
46+
: '[array]';
47+
}
48+
49+
return $b;
50+
}, $bindings);
1851
}
1952
}
2053

0 commit comments

Comments
 (0)