|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\SentryLaravel; |
| 4 | + |
| 5 | +use Illuminate\Events\Dispatcher; |
| 6 | + |
| 7 | +// Event handling inspired by the ``laravel-debugbar`` project: |
| 8 | +// https://github.com/barryvdh/laravel-debugbar |
| 9 | +class SentryLaravelEventHandler |
| 10 | +{ |
| 11 | + public function __construct(\Raven_Client $client, array $config) |
| 12 | + { |
| 13 | + $this->client = $client; |
| 14 | + $this->sqlBindings = ( |
| 15 | + isset($config['breadcrumbs.sql_bindings']) |
| 16 | + ? $config['breadcrumbs.sql_bindings'] |
| 17 | + : true |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + public function subscribe(Dispatcher $events) |
| 22 | + { |
| 23 | + $this->events = $events; |
| 24 | + $events->listen('*', [$this, 'onWildcardEvent']); |
| 25 | + } |
| 26 | + |
| 27 | + public function onWildcardEvent() |
| 28 | + { |
| 29 | + $name = $this->events->firing(); |
| 30 | + $args = func_get_args(); |
| 31 | + $data = null; |
| 32 | + $level = 'info'; |
| 33 | + if ($name === 'Illuminate\Database\Events\QueryExecuted') { |
| 34 | + $name = 'sql.query'; |
| 35 | + $message = $args[0]->sql; |
| 36 | + $data = array( |
| 37 | + 'connectionName' => $args[0]->connectionName, |
| 38 | + ); |
| 39 | + if ($this->sqlBindings) { |
| 40 | + $bindings = $args[0]->bindings; |
| 41 | + if (!empty($bindings)) { |
| 42 | + $data['bindings'] = $bindings; |
| 43 | + } |
| 44 | + } |
| 45 | + } elseif ($name === 'illuminate.query') { |
| 46 | + // $args = array(sql, bindings, ...) |
| 47 | + $name = 'sql.query'; |
| 48 | + $message = $args[0]; |
| 49 | + $data = array( |
| 50 | + 'connectionName' => $args[3], |
| 51 | + ); |
| 52 | + if ($this->sqlBindings) { |
| 53 | + $bindings = $args[1]; |
| 54 | + if (!empty($bindings)) { |
| 55 | + $data['bindings'] = $bindings; |
| 56 | + } |
| 57 | + } |
| 58 | + } elseif ($name === 'illuminate.log') { |
| 59 | + $name = 'log.' . $args[0]; |
| 60 | + $level = $args[0]; |
| 61 | + $message = $args[1]; |
| 62 | + if (!empty($args[2])) { |
| 63 | + $data = array('params' => $args[2]); |
| 64 | + } |
| 65 | + } else { |
| 66 | + return; |
| 67 | + } |
| 68 | + $this->client->breadcrumbs->record(array( |
| 69 | + 'message' => $message, |
| 70 | + 'category' => $name, |
| 71 | + 'data' => $data, |
| 72 | + 'level' => $level, |
| 73 | + )); |
| 74 | + } |
| 75 | +} |
0 commit comments