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