Skip to content

Instantly share code, notes, and snippets.

@technoknol
Last active June 22, 2017 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technoknol/cb247dc2c79048a575e1cfb8614db0c2 to your computer and use it in GitHub Desktop.
Save technoknol/cb247dc2c79048a575e1cfb8614db0c2 to your computer and use it in GitHub Desktop.
Laravel 5.4+ Query logging/ debugging/ printing via Middleware
<?php
// File location :: \app\Http\Middleware\debugger.php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Closure;
class debugger {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
DB::connection()->enableQueryLog();
return $next($request);
}
public function terminate() {
Log::debug('QUERIES__LOG_STARTED');
$queries = DB::getQueryLog();
// $formattedQueries = [];
foreach ($queries as $query) :
$prep = $query['query'];
foreach ($query['bindings'] as $binding) :
$prep = preg_replace("#\?#", $binding, $prep, 1);
endforeach;
Log::debug('Query---> ' . $prep);
// $formattedQueries[] = $prep;
endforeach;
// return $formattedQueries;
Log::debug('QUERIES__LOG_ENDED');
}
}
<?php
// File : \app\Http\Kernel.php
// Do the change as per following, read all comments
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\debugger::class // You can add middleware here so that it will be enabled for all requests. e.g. Web and API
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
'cors',
'debug' // Add here to enable only for api middleware group.
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
'cors' => \App\Http\Middleware\CORS::class,
'debug'=>\App\Http\Middleware\debugger::class // here is a debug, created alias for debug middleware.
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment