Open2
Laravel ローカル雑ログ出力

Route::matched(function ($event): void {
$controller = $event->route->getController();
$controller = is_string($controller) ? $controller : get_class($controller);
Log::debug(implode("\n", [
'ControllerListener:',
app('request')->method(),
app('request')->getPathInfo(),
$controller,
]));
});

DB::listen(function ($query): void {
$sql = $query->sql;
$sql = preg_replace_callback(
'/\b(SELECT)\b/i',
fn ($matches) => strtoupper($matches[1]) . "\n ",
$sql
);
$sql = preg_replace_callback(
'/\b(IS NULL|IS NOT NULL|IN|AS|COUNT|ASC|DESC|NOT|EXISTS|INSERT|INTO|IGNORE)\b/i',
fn ($matches) => strtoupper($matches[1]),
$sql
);
$sql = preg_replace_callback(
'/\b(FROM|LEFT JOIN|INNER JOIN|HAVING|LIMIT|OFFSET)\b/i',
fn ($matches) => "\n" . strtoupper($matches[1]),
$sql
);
$sql = preg_replace_callback(
'/\b(ON)\b/i',
fn ($matches) => "\n " . strtoupper($matches[1]) . ' ',
$sql
);
$sql = preg_replace_callback(
'/\b(WHERE)\b/i',
fn ($matches) => "\n" . strtoupper($matches[1]) . "\n ",
$sql
);
$sql = preg_replace_callback(
'/\b(AND|OR)\b/i',
fn ($matches) => "\n " . strtoupper($matches[1]),
$sql
);
$sql = preg_replace_callback(
'/\b(ORDER BY|GROUP BY)\b/i',
fn ($matches) => "\n" . strtoupper($matches[1]) . "\n ",
$sql
);
$sql = strtr($sql, [
'`' => '',
', ' => "\n ,",
'(' => "(\n ",
]);
Log::debug(implode("\n", [
'QueryListener:',
// $query->sql,
$sql,
';',
sprintf('bindings: %s', var_export($query->bindings, true)),
sprintf('time: %.2fms', $query->time),
]));
});