PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.4
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.4
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-updraft-file-logger.php

class-updraft-file-logger.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.4, at includes/class-updraft-file-logger.php

183 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 if (class_exists('Updraft_File_Logger')) return;
6
7 /**
8 * Class Updraft_File_Logger
9 */
10 class Updraft_File_Logger extends Updraft_Abstract_Logger {
11
12 /**
13 * Path to the log file
14 *
15 * @var String
16 */
17 private $logfile;
18
19 /**
20 * Updraft_File_Logger constructor
21 */
22 public function __construct($logfile) {
23 $this->logfile = $logfile;
24 }
25
26 /**
27 * Returns logger description
28 *
29 * @return string
30 */
31 public function get_description() {
32 return __('Log events into a log file', 'wp-optimize');
33 }
34
35 /**
36 * Emergency message
37 *
38 * @param string $message
39 * @param array $context
40 * @return void
41 */
42 public function emergency($message, $context = array()) {
43 $this->log($message, Updraft_Log_Levels::EMERGENCY, $context);
44 }
45
46 /**
47 * Alert message
48 *
49 * @param string $message
50 * @param array $context
51 * @return void
52 */
53 public function alert($message, $context = array()) {
54 $this->log($message, Updraft_Log_Levels::ALERT, $context);
55 }
56
57 /**
58 * Critical message
59 *
60 * @param string $message
61 * @param array $context
62 * @return void
63 */
64 public function critical($message, $context = array()) {
65 $this->log($message, Updraft_Log_Levels::CRITICAL, $context);
66 }
67
68 /**
69 * Error message
70 *
71 * @param string $message
72 * @param array $context
73 * @return void
74 */
75 public function error($message, $context = array()) {
76 $this->log($message, Updraft_Log_Levels::ERROR, $context);
77 }
78
79 /**
80 * Warning message
81 *
82 * @param string $message
83 * @param array $context
84 * @return void
85 */
86 public function warning($message, $context = array()) {
87 $this->log($message, Updraft_Log_Levels::WARNING, $context);
88 }
89
90 /**
91 * Notice message
92 *
93 * @param string $message
94 * @param array $context
95 * @return void
96 */
97 public function notice($message, $context = array()) {
98 $this->log($message, Updraft_Log_Levels::NOTICE, $context);
99 }
100
101 /**
102 * Info message
103 *
104 * @param string $message
105 * @param array $context
106 * @return void
107 */
108 public function info($message, $context = array()) {
109 $this->log($message, Updraft_Log_Levels::INFO, $context);
110 }
111
112 /**
113 * Debug message
114 *
115 * @param string $message
116 * @param array $context
117 * @return void
118 */
119 public function debug($message, $context = array()) {
120 $this->log($message, Updraft_Log_Levels::DEBUG, $context);
121 }
122
123 /**
124 * Log message with any level
125 *
126 * @param string $message
127 * @param mixed $level
128 * @param array $context
129 * @return void
130 */
131 public function log($message, $level, $context = array()) {
132
133 if (!$this->is_enabled()) return;
134
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
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 }
140 }
141
142 /**
143 * Delete logs older than specified date
144 *
145 * @param string $how_old
146 * @return boolean Success or failure
147 */
148 public function prune_logs($how_old = "5 days ago") {
149
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");
154 }
155
156 // phpcs:disable
157
158 // We ignore a few lines here to avoid warnings on file operations
159 // WP.VIP does not like us writing directly to the filesystem
160 if (!is_file($this->logfile)) return false;
161 $logfile_handle = fopen($this->logfile, "r");
162 if (false === $logfile_handle) return false;
163 $temp_file = fopen(preg_replace("/\.log$/", "-temp.log", $this->logfile), "a");
164
165 // Stream is the preferred way because of potentially large file sizes
166 while ($line = stream_get_line($logfile_handle, 1024 * 1024, "\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 }
173 }
174 }
175
176 fclose($logfile_handle);
177 fclose($temp_file);
178
179 return rename(preg_replace("/\.log$/", "-temp.log", $this->logfile), $this->logfile);
180 // phpcs:enable
181 }
182 }
183