Skip to content

Commit

Permalink
Laravel query log package. Init
Browse files Browse the repository at this point in the history
  • Loading branch information
dipenparmar12 committed Jul 14, 2020
1 parent ca9fddf commit 285ca10
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ return [
| Here you may configure the log channels for query log.
| This option defines the default log channel that gets used when writing
| Query to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
| one of the channels defined in the `config/logging.php` "channels" configuration array.
| You can define multiple channels seprated by comman (,) for query log.
|
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Exceptions/LogChannelInvalidException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Dipen Parmar.
*
* (c) Dipen Parmar <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dipenparmar12\QueryLog\Exceptions;

class LogChannelInvalidException extends QueryLogException
{
/**
* {@inheritdoc}
*/
protected $message = 'In given channels one or more channel is not defined.';
}
22 changes: 22 additions & 0 deletions src/Exceptions/QueryLogException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dipenparmar12\QueryLog\Exceptions;

use Exception;

class QueryLogException extends Exception
{
/**
* {@inheritdoc}
*/
protected $message = 'Dipenparmar12/QueryLog:, An error occurred';
}
78 changes: 78 additions & 0 deletions src/StoreQueryLogServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Dipenparmar12\QueryLog;

use DB;
use Dipenparmar12\QueryLog\Exceptions\LogChannelInvalidException;
use Log;
use Illuminate\Support\ServiceProvider;
use Throwable;

class StoreQueryLogServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/querylog_config.php' => config_path('querylog.php'),
], 'config');
}

if (config('querylog.query_log_enable') == true) {
/// Log all queries executed, performed by Application
DB::connection()->enableQueryLog();
DB::listen(function ($query) {
try {
Log::stack($this->get_log_channels())->info($this->BindQueryLog($query->sql, $query->bindings));
} catch (\Exception $t) {
throw new LogChannelInvalidException('In given channels one or more channels are not defined.');
}
});
}
}

/**
* Get log channels
* @return array
*/
protected function get_log_channels()
{
return collect(array_filter(explode(',', config('querylog.log_chhanels'))))->toArray();
# "one,two" => ['one', 'two'];
}

/**
* Bind-Query parameters in Query string
*
* @param $sql
* @param $binds
*
* @return string
*/
protected function BindQueryLog($sql, $binds)
{
$result = "";
$sql_chunks = explode('?', $sql);

foreach ($sql_chunks as $key => $sql_chunk) {
if (isset($binds[$key])) {
$result .= $sql_chunk . '"' . $binds[$key] . '"';
}
}

$result .= $sql_chunks[count($sql_chunks) - 1];
return $result;
}

/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/querylog_config.php', 'querylog');
}

}

0 comments on commit 285ca10

Please sign in to comment.