Completed
Push — master ( d38aea...aecaa2 )
by Russell
04:32 queued 11s
created

src/Handler/SentryHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class: SentryHandler.
5
 *
6
 * @author  Russell Michell 2017-2019 <[email protected]>
7
 * @package phptek/sentry
8
 */
9
10
namespace PhpTek\Sentry\Handler;
11
12
use Monolog\Handler\AbstractProcessingHandler;
13
use Monolog\Logger;
14
use Sentry\Severity;
15
use Sentry\State\Scope;
16
use SilverStripe\Core\Injector\Injectable;
17
use PhpTek\Sentry\Log\SentryLogger;
18
use PhpTek\Sentry\Adaptor\SentryAdaptor;
19
use PhpTek\Sentry\Adaptor\SentrySeverity;
20
21
/**
22
 * Monolog handler to send messages to a Sentry (https://github.com/getsentry/sentry) server
23
 * using sentry-php (https://github.com/getsentry/sentry-php).
24
 */
25
class SentryHandler extends AbstractProcessingHandler
26
{
27
28
    use Injectable;
29
30
    /**
31
     * @param  int     $level
32
     * @param  boolean $bubble
33
     * @param  array   $extras
34
     * @return void
35
     */
36
    public function __construct(int $level = Logger::DEBUG, bool $bubble = true, array $extras = [])
37
    {
38
        // Returns an instance of {@link SentryLogger}
39
        $logger = SentryLogger::factory($extras);
40
        $this->client = $logger->getAdaptor();
41
42
        parent::__construct($level, $bubble);
43
    }
44
45
    /**
46
     * write() forms the entry point into the physical sending of the error. The
47
     * sending itself is done by the current adaptor's `send()` method.
48
     *
49
     * @param  array $record An array of error-context metadata with the following
50
     *                       available keys:
51
     *
52
     *                       - message
53
     *                       - context
54
     *                       - level
55
     *                       - level_name
56
     *                       - channel
57
     *                       - datetime
58
     *                       - extra
59
     *                       - formatted
60
     *
61
     * @return void
62
     */
63
    protected function write(array $record) : void
64
    {
65
        $record = array_merge($record, [
66
            'timestamp' => $record['datetime']->getTimestamp(),
67
            'stacktrace' => SentryLogger::backtrace($record),
68
        ]);
69
70
        if (
71
                isset($record['context']['exception']) &&
72
                $record['context']['exception'] instanceof \Throwable
73
        ) {
74
            $this->client->getSDK()->captureException($record['context']['exception']);
75
        } else {
76
            $this->client->getSDK()->captureMessage(
77
                $record['formatted'],
78
                new Severity(SentrySeverity::process_severity($record['level_name'])),
79
                $this->client->getContext()
80
            );
81
        }
82
    }
83
84
    /**
85
     * @return SentryAdaptor
86
     */
87
    public function getClient() : SentryAdaptor
88
    {
89
        return $this->client;
90
    }
91
    
92
    /**
93
     * @return {@link Scope}
0 ignored issues
show
The doc-type {@link could not be parsed: Unknown type name "{@link" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
94
     */
95
    public function getMessageScope() : Scope
96
    {
97
        
98
    }
99
    
100
}
101