Created
January 30, 2015 10:06
-
-
Save linhmtran168/78a64f45be8bdcb70c86 to your computer and use it in GitHub Desktop.
Laravel log sql query
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
#app\start\global.php | |
require_once app_path('logger.php'); | |
#app\logger.php | |
use Monolog\Logger; | |
use Monolog\Handler\StreamHandler; | |
if( Config::get('app.debug') === true ){ | |
DB::listen(function($sql, $bindings, $time){ | |
$logFile = storage_path('logs/query.log'); | |
$monolog = new Logger('log'); | |
$monolog->pushHandler(new StreamHandler($logFile), Logger::INFO); | |
$monolog->info($sql, compact('bindings', 'time')); | |
}); | |
} | |
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
if (Config::get('database.log', 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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment