Created
November 20, 2014 11:23
-
-
Save patrickmaciel/04ad737f15680e19f92d to your computer and use it in GitHub Desktop.
Laravel 4.2 filter for query log
This file contains hidden or 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
<?php | |
if (Config::get('app.debug')) { | |
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 = new Logger('sql'); | |
$log->pushHandler(new StreamHandler(storage_path().'/logs/sql-' . date('Y-m-d') . '.log', Logger::INFO)); | |
// add records to the log | |
$log->addInfo($query, $data); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment