| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of Monolog Extensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @see  http://github.com/graze/MonologExtensions/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link http://github.com/graze/MonologExtensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Graze\Monolog\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\Monolog\Formatter\RaygunFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Monolog\Handler\AbstractProcessingHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Monolog\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Raygun4php\RaygunClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class RaygunHandler extends AbstractProcessingHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var RaygunClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param RaygunClient $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param int          $level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param bool         $bubble | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(RaygunClient $client, $level = Logger::DEBUG, $bubble = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $this->client = $client; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         parent::__construct($level, $bubble); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function write(array $record) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $context = $record['context']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (isset($context['exception']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             && ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $context['exception'] instanceof \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 || (PHP_VERSION_ID > 70000 && $context['exception'] instanceof \Throwable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->writeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $record, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $record['formatted']['tags'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $record['formatted']['custom_data'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $record['formatted']['timestamp'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } elseif (isset($context['file']) && isset($context['line'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->writeError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $record['formatted'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $record['formatted']['tags'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $record['formatted']['custom_data'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $record['formatted']['timestamp'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // do nothing if its not an exception or an error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param array     $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array     $tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param array     $customData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param int|float $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     protected function writeError(array $record, array $tags = [], array $customData = [], $timestamp = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $context = $record['context']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->client->SendError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $record['message'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $context['file'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $context['line'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $tags, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $customData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $timestamp | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param array     $record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param array     $tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param array     $customData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param int|float $timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     protected function writeException(array $record, array $tags = [], array $customData = [], $timestamp = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->client->SendException($record['context']['exception'], $tags, $customData, $timestamp); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return \Monolog\Formatter\FormatterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     protected function getDefaultFormatter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return new RaygunFormatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  |