| @@ -25,9 +25,9 @@ | ||
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Returns logger description |
| 28 | 28 | * |
| 29 | - * @return string|void | |
| 29 | + * @return string | |
| 30 | 30 | */ |
| 31 | 31 | public function get_description() { |
| 32 | 32 | return __('Log events into a log file', 'wp-optimize'); |
| 33 | 33 | } |
| @@ -36,11 +36,11 @@ | ||
| 36 | 36 | * Emergency message |
| 37 | 37 | * |
| 38 | 38 | * @param string $message |
| 39 | 39 | * @param array $context |
| 40 | - * @return null|void | |
| 40 | + * @return void | |
| 41 | 41 | */ |
| 42 | - public function emergency($message, array $context = array()) { | |
| 42 | + public function emergency($message, $context = array()) { | |
| 43 | 43 | $this->log($message, Updraft_Log_Levels::EMERGENCY, $context); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
| @@ -47,11 +47,11 @@ | ||
| 47 | 47 | * Alert message |
| 48 | 48 | * |
| 49 | 49 | * @param string $message |
| 50 | 50 | * @param array $context |
| 51 | - * @return null|void | |
| 51 | + * @return void | |
| 52 | 52 | */ |
| 53 | - public function alert($message, array $context = array()) { | |
| 53 | + public function alert($message, $context = array()) { | |
| 54 | 54 | $this->log($message, Updraft_Log_Levels::ALERT, $context); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| @@ -58,11 +58,11 @@ | ||
| 58 | 58 | * Critical message |
| 59 | 59 | * |
| 60 | 60 | * @param string $message |
| 61 | 61 | * @param array $context |
| 62 | - * @return null|void | |
| 62 | + * @return void | |
| 63 | 63 | */ |
| 64 | - public function critical($message, array $context = array()) { | |
| 64 | + public function critical($message, $context = array()) { | |
| 65 | 65 | $this->log($message, Updraft_Log_Levels::CRITICAL, $context); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| @@ -69,11 +69,11 @@ | ||
| 69 | 69 | * Error message |
| 70 | 70 | * |
| 71 | 71 | * @param string $message |
| 72 | 72 | * @param array $context |
| 73 | - * @return null|void | |
| 73 | + * @return void | |
| 74 | 74 | */ |
| 75 | - public function error($message, array $context = array()) { | |
| 75 | + public function error($message, $context = array()) { | |
| 76 | 76 | $this->log($message, Updraft_Log_Levels::ERROR, $context); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
| @@ -80,11 +80,11 @@ | ||
| 80 | 80 | * Warning message |
| 81 | 81 | * |
| 82 | 82 | * @param string $message |
| 83 | 83 | * @param array $context |
| 84 | - * @return null|void | |
| 84 | + * @return void | |
| 85 | 85 | */ |
| 86 | - public function warning($message, array $context = array()) { | |
| 86 | + public function warning($message, $context = array()) { | |
| 87 | 87 | $this->log($message, Updraft_Log_Levels::WARNING, $context); |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
| @@ -91,11 +91,11 @@ | ||
| 91 | 91 | * Notice message |
| 92 | 92 | * |
| 93 | 93 | * @param string $message |
| 94 | 94 | * @param array $context |
| 95 | - * @return null|void | |
| 95 | + * @return void | |
| 96 | 96 | */ |
| 97 | - public function notice($message, array $context = array()) { | |
| 97 | + public function notice($message, $context = array()) { | |
| 98 | 98 | $this->log($message, Updraft_Log_Levels::NOTICE, $context); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
| @@ -102,11 +102,11 @@ | ||
| 102 | 102 | * Info message |
| 103 | 103 | * |
| 104 | 104 | * @param string $message |
| 105 | 105 | * @param array $context |
| 106 | - * @return null|void | |
| 106 | + * @return void | |
| 107 | 107 | */ |
| 108 | - public function info($message, array $context = array()) { | |
| 108 | + public function info($message, $context = array()) { | |
| 109 | 109 | $this->log($message, Updraft_Log_Levels::INFO, $context); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| @@ -113,11 +113,11 @@ | ||
| 113 | 113 | * Debug message |
| 114 | 114 | * |
| 115 | 115 | * @param string $message |
| 116 | 116 | * @param array $context |
| 117 | - * @return null|void | |
| 117 | + * @return void | |
| 118 | 118 | */ |
| 119 | - public function debug($message, array $context = array()) { | |
| 119 | + public function debug($message, $context = array()) { | |
| 120 | 120 | $this->log($message, Updraft_Log_Levels::DEBUG, $context); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
| @@ -125,18 +125,18 @@ | ||
| 125 | 125 | * |
| 126 | 126 | * @param string $message |
| 127 | 127 | * @param mixed $level |
| 128 | 128 | * @param array $context |
| 129 | - * @return null|void | |
| 129 | + * @return void | |
| 130 | 130 | */ |
| 131 | - public function log($message, $level, array $context = array()) { | |
| 131 | + public function log($message, $level, $context = array()) { | |
| 132 | 132 | |
| 133 | - if (!$this->is_enabled()) return false; | |
| 133 | + if (!$this->is_enabled()) return; | |
| 134 | 134 | |
| 135 | - $message = sprintf("[%s : %s] - %s \n", date("Y-m-d H:i:s"), Updraft_Log_Levels::to_text($level), $this->interpolate($message, $context)); | |
| 135 | + $message = sprintf("[%s : %s] - %s \n", gmdate("Y-m-d H:i:s"), Updraft_Log_Levels::to_text($level), $this->interpolate($message, $context)); | |
| 136 | 136 | |
| 137 | - if (false == file_put_contents($this->logfile, $message, FILE_APPEND)) { | |
| 138 | - error_log($message); | |
| 137 | + if (false === file_put_contents($this->logfile, $message, FILE_APPEND)) { | |
| 138 | + error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Using for debugging purpose | |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
| @@ -146,10 +146,12 @@ | ||
| 146 | 146 | * @return boolean Success or failure |
| 147 | 147 | */ |
| 148 | 148 | public function prune_logs($how_old = "5 days ago") { |
| 149 | 149 | |
| 150 | - if (strtotime($how_old)) { | |
| 151 | - $how_old = "5 days ago"; | |
| 150 | + // If the $how_old string is invalid revert to default "5 days ago" | |
| 151 | + $prune_period = strtotime($how_old); | |
| 152 | + if (!$prune_period) { | |
| 153 | + $prune_period = strtotime("5 days ago"); | |
| 152 | 154 | } |
| 153 | 155 | |
| 154 | 156 | // phpcs:disable |
| 155 | 157 | |
| @@ -154,17 +156,21 @@ | ||
| 154 | 156 | // phpcs:disable |
| 155 | 157 | |
| 156 | 158 | // We ignore a few lines here to avoid warnings on file operations |
| 157 | 159 | // WP.VIP does not like us writing directly to the filesystem |
| 160 | + if (!is_file($this->logfile)) return false; | |
| 158 | 161 | $logfile_handle = fopen($this->logfile, "r"); |
| 162 | + if (false === $logfile_handle) return false; | |
| 159 | 163 | $temp_file = fopen(preg_replace("/\.log$/", "-temp.log", $this->logfile), "a"); |
| 160 | 164 | |
| 161 | 165 | // Stream is the preferred way because of potentially large file sizes |
| 162 | 166 | while ($line = stream_get_line($logfile_handle, 1024 * 1024, "\n")) { |
| 163 | - $entry_time = strtotime(strstr($line, " : ", true)); | |
| 164 | - | |
| 165 | - if ($entry_time > $how_old) { | |
| 166 | - fwrite($temp_file, $line."\n"); | |
| 167 | + | |
| 168 | + $pattern = '/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/'; | |
| 169 | + if (preg_match($pattern, $line, $matches)) { | |
| 170 | + if (strtotime($matches[0]) > $prune_period) { | |
| 171 | + fwrite($temp_file, $line."\n"); | |
| 172 | + } | |
| 167 | 173 | } |
| 168 | 174 | } |
| 169 | 175 | |
| 170 | 176 | fclose($logfile_handle); |