-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca9fddf
commit 285ca10
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |