Skip to content

Commit

Permalink
Ready to publish.
Browse files Browse the repository at this point in the history
  • Loading branch information
dipenparmar12 committed Jul 18, 2020
1 parent 7fd7209 commit ed99f64
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 40 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -41,7 +40,7 @@ return [
|
*/

'log_chhanels' => env('QUERY_LOG_CHHANELS', "single"),
'log_chhanels' => env('QUERY_LOG_CHHANELS', null),

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion config/querylog_config.php → config/querylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
|
*/

'log_chhanels' => env('QUERY_LOG_CHHANELS', "single"),
'log_chhanels' => env('QUERY_LOG_CHHANELS', null),

/*
|--------------------------------------------------------------------------
Expand Down
20 changes: 0 additions & 20 deletions src/Exceptions/LogChannelInvalidException.php

This file was deleted.

66 changes: 50 additions & 16 deletions src/StoreQueryLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
Expand All @@ -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
)
]);
}

/**
Expand All @@ -52,27 +82,31 @@ 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;*/
}

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

}

0 comments on commit ed99f64

Please sign in to comment.