12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace App\Listeners;
- use Illuminate\Database\Events\QueryExecuted;
- use Illuminate\Support\Facades\Log;
- use Monolog\Handler\RotatingFileHandler;
- use Monolog\Logger;
- class QueryListener
- {
- /**
- * Create the event listener.
- *
- * @return void
- */
- public function __construct()
- {
- //
- }
- /**
- * Handle the event.
- *
- * @param QueryExecuted $event
- * @return void
- */
- public function handle(QueryExecuted $event)
- {
- try{
- if (env('APP_DEBUG') == true) {
- $this->recordLog($event, 'logs/sql/sql.log');
- }
- }catch (\Exception $exception){
- Log::error('log sql error:'.$exception->getMessage());
- }
- }
- protected function recordLog($event, $sqlFile)
- {
- $sql = str_replace("?", "'%s'", $event->sql);
- foreach ($event->bindings as $i => $binding) {
- if ($binding instanceof \DateTime) {
- $event->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
- } else {
- if (is_string($binding)) {
- $event->bindings[$i] = "'$binding'";
- }
- }
- }
- $log = vsprintf($sql, $event->bindings);
- $log = $log.' [ RunTime:'.$event->time.'ms ] ';
- (new Logger('sql'))->pushHandler(new RotatingFileHandler(storage_path($sqlFile)))->info($log);
- }
- }
|