Skip to content

Commit

Permalink
feat: slow log and change log format
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Mar 25, 2022
1 parent 5c6bbe3 commit 1fcfa44
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
4 changes: 3 additions & 1 deletion config/eloquent-dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
'grammar' => env('ELOQUENT_DUMPER_GRAMMAR', 'default'),
'logging' => [
'format' => '[%connection-name%] [%time%] %sql% | %method% %uri%',
'format' => "\nurl: %uri%\nmethod: %method%\nconnection: %connection-name%\ntime: %time%\nquery: %formatted_sql%",
'pattern' => '#(select).*#i',
'slow_query_exec_time' => env('ELOQUENT_DUMPER_SLOW_QUERY_EXEC_TIME', 3),
'channels' => [
'log' => [
'driver' => 'single',
Expand Down
7 changes: 7 additions & 0 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Recca0120\EloquentDumper;

use DateTime;
use Doctrine\SqlFormatter\Highlighter;
use Doctrine\SqlFormatter\SqlFormatter;
use Illuminate\Database\Query\Expression;
use PDO;
use Recca0120\EloquentDumper\Dumpers\MySqlDumper;
Expand Down Expand Up @@ -75,6 +77,11 @@ public static function factory(?string $driver = null): self
return new $grammar();
}

public static function format(string $sql, Highlighter $highlighter = null): string
{
return (new SqlFormatter($highlighter))->format($sql);
}

/**
* @param string $sql
* @return string
Expand Down
48 changes: 33 additions & 15 deletions src/EloquentDumperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Recca0120\EloquentDumper;

use Closure;
use Doctrine\SqlFormatter\NullHighlighter;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\Query\Builder;
Expand Down Expand Up @@ -30,19 +31,9 @@ public function register()
});

$this->app->singleton(OutputInterface::class, function () {
return $this->app->runningInConsole() ? new ConsoleOutput() : new SymfonyDumpOutput();
});

$this->registerBuilderMicro('toRawSql', function () {
return app(Dumper::class)
->setPdo($this->getConnection()->getPdo())
->dump($this->toSql(), $this->getBindings());
});

$this->registerBuilderMicro('dumpSql', function () {
app(OutputInterface::class)->output($this->toRawSql());

return $this;
return $this->app->runningInConsole()
? new ConsoleOutput()
: new SymfonyDumpOutput();
});
}

Expand All @@ -59,29 +50,56 @@ public function boot()
], 'eloquent-dumper');
}

$this->registerBuilderMicro('toRawSql', function () {
$pdo = $this->getConnection()->getPdo();
$sql = $this->toSql();
$bindings = $this->getBindings();

return app(Dumper::class)->setPdo($pdo)->dump($sql, $bindings);
});

$this->registerBuilderMicro('dumpSql', function () {
app(OutputInterface::class)->output($this->toRawSql());

return $this;
});

$channels = $this->getConfig('logging.channels');
foreach ($channels as $name => $channel) {
$name = 'logging.channels.eloquent-dumper-'.$name;
$this->app['config']->set($name, $channel);
}

$format = $this->getConfig('logging.format');
$pattern = $this->getConfig('logging.pattern');
$slowQueryExecTime = $this->getConfig('slow_query_exec_time');

DB::listen(static function (QueryExecuted $event) use ($format) {
DB::listen(static function (QueryExecuted $event) use ($format, $pattern, $slowQueryExecTime) {
$request = app(Request::class);
$sql = app(Dumper::class)
->setPdo($event->connection->getPdo())
->dump($event->sql, $event->bindings);

if (! preg_match($pattern, $sql)) {
return;
}

$attributes = [
'%connection-name%' => $event->connectionName,
'%time%' => static::formatDuration($event->time),
'%sql%' => $sql,
'%formatted_sql%' => Dumper::format($sql, new NullHighlighter()),
'%method%' => $request->method(),
'%uri%' => $request->getRequestUri(),
];

Log::channel('eloquent-dumper-log')->debug(strtr($format, $attributes));
Log::channel('eloquent-dumper-log')
->debug(strtr($format, $attributes));

if ($event->time > $slowQueryExecTime) {
Log::channel('eloquent-dumper-slow-log')
->debug(strtr($format, $attributes));
}
});
}

Expand Down
13 changes: 2 additions & 11 deletions src/Output/ConsoleOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@

namespace Recca0120\EloquentDumper\Output;

use Doctrine\SqlFormatter\SqlFormatter;
use Recca0120\EloquentDumper\Dumper;

class ConsoleOutput implements OutputInterface
{
public function output(string $sql): void
{
echo "\n".self::format($sql)."\n";
}

/**
* @param string $sql
* @return string
*/
private static function format(string $sql): string
{
return (new SqlFormatter())->format($sql);
echo "\n".Dumper::format($sql)."\n";
}
}

0 comments on commit 1fcfa44

Please sign in to comment.