Skip to content

Commit

Permalink
allow for query logger to write to different logs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewtrask committed Dec 10, 2020
1 parent b60012a commit eddeac6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 8 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* If this is set to "null", the app.debug config value will be used.
*/
'enabled' => env('QUERY_DETECTOR_ENABLED', null),

/*
* Threshold level for the N+1 query detection. If a relation query will be
* executed more then this amount, the detector will notify you about it.
Expand All @@ -27,6 +27,13 @@
//]
],

/*
* Here you can set a specific log channel to write to
* in case you are trying to isolate queries or have a lot
* going on in the laravel.log. Defaults to laravel.log though.
*/
'log_channel' => env('QUERY_DETECTOR_LOG_CHANNEL', 'daily'),

/*
* Define the output format that you want to use. Multiple classes are supported.
* Available options are:
Expand Down
13 changes: 9 additions & 4 deletions src/Outputs/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ public function boot()

public function output(Collection $detectedQueries, Response $response)
{
LaravelLog::info('Detected N+1 Query');
$this->log('Detected N+1 Query');

foreach ($detectedQueries as $detectedQuery) {
$logOutput = 'Model: '.$detectedQuery['model'] . PHP_EOL;

$logOutput .= 'Relation: '.$detectedQuery['relation'] . PHP_EOL;

$logOutput .= 'Num-Called: '.$detectedQuery['count'] . PHP_EOL;

$logOutput .= 'Call-Stack:' . PHP_EOL;

foreach ($detectedQuery['sources'] as $source) {
$logOutput .= '#'.$source->index.' '.$source->name.':'.$source->line . PHP_EOL;
}

LaravelLog::info($logOutput);
$this->log($logOutput);
}
}

private function log(string $message)
{
LaravelLog::channel(config('querydetector.log_channel'))->info($message);
}
}

0 comments on commit eddeac6

Please sign in to comment.