| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Phoole (PHP7.2+) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @category  Library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package   Phoole\Logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2019 Hong Zhang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Phoole\Logger\Handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Phoole\Logger\Formatter\FormatterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * log to a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @package Phoole\Logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class LogfileHandler extends StreamHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * file rotation type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @const int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const ROTATE_NONE = 0; // do not rotate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     const ROTATE_DATE = 1; // rotate by date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param  string             $path    full path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param  FormatterInterface $formatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param  int                $rotate  rotate type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @throws \LogicException if path not writable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         string $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ?FormatterInterface $formatter = NULL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         int $rotate = self::ROTATE_NONE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->checkPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (file_exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->doRotation($path, $rotate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         parent::__construct($path, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Check file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @throws \LogicException if directory failure etc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected function checkPath(string $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $dir = dirname($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             if (!is_dir($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 mkdir($dir, 0777, TRUE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if (!is_dir($dir) || !is_writable($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 throw new \Exception("unable to write to $path"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new \LogicException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Rotate file on start | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param  int    $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function doRotation(string $path, int $type): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             // by date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             case self::ROTATE_DATE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 return $this->rotateByDate($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // no rotation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 return TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Rotate $path to $path_20160616 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param  string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param  string $format  date format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     protected function rotateByDate(string $path, string $format = 'Ymd'): bool | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         $time = filemtime($path); | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         if ($time < strtotime('today')) { | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             return rename($path, $path . '_' . date($format, $time)); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |             return FALSE; | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } |