PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
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 3.2.21, at includes/class-updraft-file-logger.php

177 lines 3.9 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|void
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 null|void
41 */
42 public function emergency($message, array $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 null|void
52 */
53 public function alert($message, array $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 null|void
63 */
64 public function critical($message, array $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 null|void
74 */
75 public function error($message, array $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 null|void
85 */
86 public function warning($message, array $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 null|void
96 */
97 public function notice($message, array $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 null|void
107 */
108 public function info($message, array $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 null|void
118 */
119 public function debug($message, array $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 null|void
130 */
131 public function log($message, $level, array $context = array()) {
132
133 if (!$this->is_enabled()) return false;
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));
136
137 if (false == file_put_contents($this->logfile, $message, FILE_APPEND)) {
138 error_log($message);
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 (strtotime($how_old)) {
151 $how_old = "5 days ago";
152 }
153
154 // phpcs:disable
155
156 // We ignore a few lines here to avoid warnings on file operations
157 // WP.VIP does not like us writing directly to the filesystem
158 $logfile_handle = fopen($this->logfile, "r");
159 $temp_file = fopen(preg_replace("/\.log$/", "-temp.log", $this->logfile), "a");
160
161 // Stream is the preferred way because of potentially large file sizes
162 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 }
169
170 fclose($logfile_handle);
171 fclose($temp_file);
172
173 return rename(preg_replace("/\.log$/", "-temp.log", $this->logfile), $this->logfile);
174 // phpcs:enable
175 }
176 }
177