forked from rrelmy/statamic-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentryServiceProvider.php
More file actions
105 lines (88 loc) · 2.55 KB
/
SentryServiceProvider.php
File metadata and controls
105 lines (88 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @author Rémy M. Böhler
*/
namespace Statamic\Addons\Sentry;
use Sentry\SentryLaravel\SentryFacade;
use Sentry\SentryLaravel\SentryLaravelServiceProvider;
use Statamic\Extend\Extensible;
/**
* Class SentryServiceProvider.
* @package Statamic\Addons\Sentry
*/
class SentryServiceProvider extends SentryLaravelServiceProvider
{
use Extensible;
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
if (!$this->enabled()) {
return;
}
parent::boot();
}
public function register()
{
if (!$this->enabled()) {
return;
}
// set config
config([
'sentry.dsn' => $this->getConfig('dsn', env('SENTRY_DSN')),
'breadcrumbs.sql_bindings' => false,
'user_context' => false, // does not work
]);
/**
* TODO add some more context
* - user
* - statamic version.
*/
// register classes
$this->app->alias('Sentry', SentryFacade::class);
// override exception handler
$this->app->singleton(
'Illuminate\Contracts\Debug\ExceptionHandler',
'Statamic\Addons\Sentry\ExceptionHandler'
);
parent::register();
if (!$this->logsEnabled()) {
$monolog = \Log::getMonolog();
// register a monolog handler to report log entries to Sentry
$handler = new \Monolog\Handler\RavenHandler(app('sentry'));
$handler->setFormatter(new \Monolog\Formatter\LineFormatter("%message% %context% %extra%\n"));
$monolog->pushHandler($handler);
$monolog->pushProcessor(function ($record) {
// add contex to the log record
$record['context']['user'] = SentryContext::getUserContext();
$record['context']['tags'] = SentryContext::getTagsContext();
return $record;
});
}
}
/**
* Check if sentry should be enabled.
*
* @return bool
*/
private function enabled()
{
return
$this->getConfigBool('enable', false) &&
in_array(app()->environment(), $this->getConfig('environments', []));
}
/**
* Check if logs should be reported.
*
* @return bool
*/
private function logsEnabled()
{
return
$this->getConfigBool('enable_logs', false) &&
in_array(app()->environment(), $this->getConfig('environments', []));
}
}