Query Stack trace listener
refactored eloquent mysql query listener and added stack trace. Change-Id: I1e152a7254a9eb3c57de35596af1e2e6c73b70f0
This commit is contained in:
parent
470dc12f5c
commit
7a3ba45d5f
65
app/Listeners/QueryExecutedListener.php
Normal file
65
app/Listeners/QueryExecutedListener.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php namespace App\Listeners;
|
||||
|
||||
use Illuminate\Database\Events\QueryExecuted;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Class QueryExecutedListener
|
||||
* @package App\Listeners
|
||||
*/
|
||||
class QueryExecutedListener
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param QueryExecuted $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(QueryExecuted $event)
|
||||
{
|
||||
if(Config::get("server.db_log_enabled", false)) {
|
||||
|
||||
$query = $event->sql;
|
||||
$bindings = $event->bindings;
|
||||
|
||||
// Format binding data for sql insertion
|
||||
foreach ($bindings as $i => $binding) {
|
||||
if ($binding instanceof DateTime) {
|
||||
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
|
||||
} else {
|
||||
if (is_string($binding)) {
|
||||
$bindings[$i] = "'$binding'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$time = $event->time;
|
||||
$connection = $event->connectionName;
|
||||
$data = compact('bindings', 'time', 'connection');
|
||||
// Insert bindings into query
|
||||
$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
|
||||
$query = vsprintf($query, $bindings);
|
||||
Log::info($query, $data);
|
||||
|
||||
//trace
|
||||
if (Config::get("server.db_log_trace_enabled", false)){
|
||||
$trace = '';
|
||||
$entries = debug_backtrace();
|
||||
unset($entries[0]);
|
||||
foreach ($entries as $entry) {
|
||||
if (!isset($entry['file']) || !isset($entry['line'])) continue;
|
||||
$trace .= $entry['file'] . ' ' . $entry['line'] . PHP_EOL;
|
||||
}
|
||||
Log::debug($trace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,15 @@
|
||||
<?php namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Monolog\Handler\NativeMailerHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
|
||||
/**
|
||||
* Class AppServiceProvider
|
||||
* @package App\Providers
|
||||
*/
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
@ -35,31 +35,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
$mono_log->pushHandler($handler);
|
||||
}
|
||||
|
||||
if(Config::get("server.db_log_enabled", false)) {
|
||||
|
||||
Event::listen('illuminate.query', function ($query, $bindings, $time, $name) {
|
||||
$data = compact('bindings', 'time', 'name');
|
||||
|
||||
// Format binding data for sql insertion
|
||||
foreach ($bindings as $i => $binding) {
|
||||
if ($binding instanceof \DateTime) {
|
||||
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
|
||||
} else {
|
||||
if (is_string($binding)) {
|
||||
$bindings[$i] = "'$binding'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert bindings into query
|
||||
$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
|
||||
$query = vsprintf($query, $bindings);
|
||||
|
||||
Log::info($query, $data);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Validator::extend('int_array', function($attribute, $value, $parameters, $validator)
|
||||
{
|
||||
$validator->addReplacer('int_array', function($message, $attribute, $rule, $parameters) use ($validator) {
|
||||
@ -73,7 +48,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
Validator::extend('text', function($attribute, $value, $parameters, $validator)
|
||||
{
|
||||
$validator->addReplacer('text', function($message, $attribute, $rule, $parameters) use ($validator) {
|
||||
@ -83,7 +57,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
return preg_match('/^[^<>\"\']+$/u', $value);
|
||||
});
|
||||
|
||||
|
||||
Validator::extend('string_array', function($attribute, $value, $parameters, $validator)
|
||||
{
|
||||
$validator->addReplacer('string_array', function($message, $attribute, $rule, $parameters) use ($validator) {
|
||||
|
@ -26,12 +26,13 @@ class EventServiceProvider extends ServiceProvider
|
||||
* @var array
|
||||
*/
|
||||
protected $listen = [
|
||||
'App\Events\SomeEvent' => [
|
||||
'App\Listeners\EventListener',
|
||||
'Illuminate\Database\Events\QueryExecuted' => [
|
||||
'App\Listeners\QueryExecutedListener',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* Register any other events for your application.
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
* @return void
|
||||
|
@ -16,6 +16,7 @@ return array
|
||||
(
|
||||
'ssl_enabled' => env('SSL_ENABLED', false),
|
||||
'db_log_enabled' => env('DB_LOG_ENABLED', false),
|
||||
'db_log_trace_enabled' => env('DB_LOG_TRACE_ENABLED', false),
|
||||
'access_token_cache_lifetime' => env('ACCESS_TOKEN_CACHE_LIFETIME', 300),
|
||||
'assets_base_url' => env('ASSETS_BASE_URL', null),
|
||||
'response_cache_lifetime' => env('API_RESPONSE_CACHE_LIFETIME', 300),
|
||||
|
Loading…
x
Reference in New Issue
Block a user