| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Rap2hpoutre\LaravelEpilog; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\ServiceProvider; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class LaravelEpilogServiceProvider extends ServiceProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |      * Bootstrap the application services. | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function boot() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->publishes(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             __DIR__ . '/../../config/config.php' => config_path('epilog.php') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $logger = \Log::getMonolog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         // Additional info in message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $logger->pushProcessor(function($record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $info = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             if (\Auth::check()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 $info .= 'User #' . \Auth::user()->id . ' (' . \Auth::user()->email . ') - '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             if (isset($_SERVER['REMOTE_ADDR'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 $info .= 'IP: ' . $_SERVER['REMOTE_ADDR']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             if (isset($_SERVER['REQUEST_URI'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $info .= "\n" . $_SERVER['REQUEST_METHOD'] . " " . url($_SERVER['REQUEST_URI']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             if (isset($_SERVER['HTTP_REFERER'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 $info .= "\nReferer: " . $_SERVER['HTTP_REFERER']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             if ($info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 $info = "\n---\n$info\n---"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 if (strpos($record['message'], "\n")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     $record['message'] = preg_replace("/\n/", $info . "\n", $record['message'], 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     $record['message'] .= $info . "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // Slack notification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (app()->environment(config('epilog.slack.env'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $slackHandler = new \Monolog\Handler\SlackHandler( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 config('epilog.slack.token'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 config('epilog.slack.channel'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 config('epilog.slack.username'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 ':skull:', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 \Monolog\Logger::ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $logger->pushHandler($slackHandler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * Register the application services. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: