diff --git a/README.md b/README.md index d20d867..d4ff5f1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Laravel Query Log [![Latest Version on Packagist](https://img.shields.io/packagist/v/dipenparmar12/laravel-query-log.svg?style=flat-square)](https://packagist.org/packages/dipenparmar12/laravel-query-log) -[![Build Status](https://img.shields.io/travis/dipenparmar12/laravel-query-log/master.svg?style=flat-square)](https://travis-ci.org/dipenparmar12/laravel-query-log) [![Total Downloads](https://img.shields.io/packagist/dt/dipenparmar12/laravel-query-log.svg?style=flat-square)](https://packagist.org/packages/dipenparmar12/laravel-query-log) A Laravel package for log queries in user defined log channel. Your all queries will save in the log file, and you can view it anytime. @@ -11,7 +10,7 @@ A Laravel package for log queries in user defined log channel. Your all queries Install the package via composer: ```bash -composer require dipenparmar12/laravel-query-log --dev +composer require dipenparmar12/laravel-query-log ``` > **Note**: Make sure this package used only in development environment, Otherwise may you face decreased performance in production. @@ -41,7 +40,7 @@ return [ | */ - 'log_chhanels' => env('QUERY_LOG_CHHANELS', "single"), + 'log_chhanels' => env('QUERY_LOG_CHHANELS', null), /* |-------------------------------------------------------------------------- diff --git a/config/querylog_config.php b/config/querylog.php similarity index 94% rename from config/querylog_config.php rename to config/querylog.php index cd6210a..d32d900 100644 --- a/config/querylog_config.php +++ b/config/querylog.php @@ -13,7 +13,7 @@ | */ - 'log_chhanels' => env('QUERY_LOG_CHHANELS', "single"), + 'log_chhanels' => env('QUERY_LOG_CHHANELS', null), /* |-------------------------------------------------------------------------- diff --git a/src/Exceptions/LogChannelInvalidException.php b/src/Exceptions/LogChannelInvalidException.php deleted file mode 100644 index e814392..0000000 --- a/src/Exceptions/LogChannelInvalidException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * 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.'; -} diff --git a/src/StoreQueryLogServiceProvider.php b/src/StoreQueryLogServiceProvider.php index 2c6ec37..0632c5d 100644 --- a/src/StoreQueryLogServiceProvider.php +++ b/src/StoreQueryLogServiceProvider.php @@ -3,32 +3,46 @@ namespace Dipenparmar12\QueryLog; use DB; -use Dipenparmar12\QueryLog\Exceptions\LogChannelInvalidException; -use Log; +use Dipenparmar12\QueryLog\Exceptions\QueryLogException; +use Exception; +use Illuminate\Config\Repository; +use Illuminate\Log\LogManager; use Illuminate\Support\ServiceProvider; -use Throwable; +use Log; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; class StoreQueryLogServiceProvider extends ServiceProvider { + public $configRepository; + /** * Bootstrap the application services. */ - public function boot() + public function boot(Repository $configRepository) { + $this->configRepository = $configRepository; + if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__.'/../config/querylog_config.php' => config_path('querylog.php'), + __DIR__ . '/../config/querylog.php' => config_path('querylog.php'), ], 'config'); } + $this->get_log_channels(); + 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.'); + if (config('querylog.log_chhanels') == null) { + $this->defaultQueryLogger()->info($this->BindQueryLog($query->sql, $query->bindings)); + } else { + Log::stack($this->get_log_channels())->info($this->BindQueryLog($query->sql, $query->bindings)); + } + } catch (Exception $t) { + throw new QueryLogException($t->getMessage()); } }); } @@ -40,8 +54,24 @@ public function boot() */ protected function get_log_channels() { - return collect(array_filter(explode(',', config('querylog.log_chhanels'))))->toArray(); - # "one,two" => ['one', 'two']; + $app_channels = collect($this->configRepository->get('logging')['channels'])->pluck('driver'); + $user_defined_channels = collect(array_filter(explode(',', config('querylog.log_chhanels')))); + return $app_channels->intersect($user_defined_channels)->toArray(); + # (string)"one,two" => ['one', 'two']; + } + + /** + * By default log queries in db-query log file + * @return Logger + */ + public function defaultQueryLogger() + { + return new Logger('Query', [ + new StreamHandler( + $this->app->storagePath() . '/logs/db-query.log' + , Logger::DEBUG + ) + ]); } /** @@ -52,19 +82,23 @@ protected function get_log_channels() * * @return string */ - protected function BindQueryLog($sql, $binds) + public function BindQueryLog($sql, $binds) { - $result = ""; - $sql_chunks = explode('?', $sql); + if (empty($binds)) { + return $sql; + } + $sql = str_replace(['%', '?'], ['%%', '%s'], $sql); + return vsprintf($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; + return $result;*/ } /** @@ -72,7 +106,7 @@ protected function BindQueryLog($sql, $binds) */ public function register() { - $this->mergeConfigFrom(__DIR__.'/../config/querylog_config.php', 'querylog'); + $this->mergeConfigFrom(__DIR__ . '/../config/querylog.php', 'querylog'); } } \ No newline at end of file