-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.class.php
More file actions
326 lines (284 loc) · 6.23 KB
/
log.class.php
File metadata and controls
326 lines (284 loc) · 6.23 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Log class
*
* LICENSE
*
* This source file is subject to the new BSD license that is available
* through the world-wide-web at this URL:
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to info@simonedamico.com so I can send you a copy immediately.
*
* @category Log
* @package Log
* @copyright Copyright (c) 2010 Simone D'Amico (http://simonedamico.com)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version 1.00 (2010-10-27)
*/
class Log {
/**
* Directory where log file will be stored
*
* @access private
*/
private $directory;
/**
* Log filename
*
* @access private
*/
private $filename;
/**
* Complete Path
*
* @access private
*/
private $logpath;
/**
* Handle for open logfile
*
* @access private
*/
private $handle;
/**
* Max size for log file (in byte)
*
* @access private
*/
private $maxfilesize; //1048576 bytes = 1MB
/**
* Action to do when the maximum size is exceeded
*
* Values allowed: (delete, rename)
*
* @access private
*/
private $exceeded_size;
/**
* Datetime formatting (http://php.net/manual/en/function.date.php)
*
* @access private
*/
private $datetime_format;
/**
* Default timezone
*
* @access private
*/
private $default_timezone = "Europe/London";
/**
* Log types
*
* @access private
*/
private $log_types = array(
'I' => '[INFO] :',
'W' => '[WARNING]:',
'E' => '[ERROR] :',
'D' => '[DEBUG] :',
'P' => '[PROFILE]:'
);
/**
* Unix timestamp for profiling
*
* @access private
*/
private $start_profile = NULL;
/**
* Constructor
*
* @param string $directory
* @param string $filename
* @param string $datetime
* @param string $maxfilesize (in MB)
* @param string $exceeded_size
*/
function __construct( $directory = "log/", $filename = "app.log", $datetime = "c", $maxfilesize = "1", $exceeded_size = "rename") {
$this->directory = $directory;
$this->filename = $filename;
$this->datetime_format = $datetime;
$this->set_maxfilesize($maxfilesize);
$this->set_exceeded_size($exceeded_size);
$this->logpath = $directory.$filename;
$this->set_default_timezone();
$this->open();
}
/**
* Destructor
*
*/
function __destruct() {
$this->close();
unset($this->start_profile);
}
/**
* Open the log file
*
* If the filesize exceed the max file size the function provides to
* rename or remove the old file.
*
* @return handle to log file
*/
function open() {
if( file_exists($this->logpath) ) {
if($this->filesize() >= $this->maxfilesize) {
if($this->exceeded_size=='delete') {
$this->handle = fopen($this->logpath, "w+") or die('Error while trying to open file specified');
} else {
$newfilename = $this->logpath.time();
if( rename($this->logpath,$newfilename) )
$this->handle = fopen($this->logpath, "a+") or die('Error while trying to open file specified');
else die('Error while trying to rename old file');
}
}
else $this->handle = fopen($this->logpath, "a+") or die('Error while trying to open file specified');
} else $this->handle = fopen($this->logpath, "w+") or die('Error while trying to open file specified');
}
/**
* Close the log file
*
*/
function close() {
fclose($this->handle);
}
/**
* Return the file size (in byte)
*
* @return filesize
*/
function filesize() {
return filesize($this->logpath);
}
/**
* Return the date with the datetime_format
*
* @return string $datetime
*/
function datetime() {
return date($this->datetime_format);
}
/**
* Set max file size for log file
*
* @param int $size (in MB)
*
* @return int $maxfilesize (in bytes)
*/
function set_maxfilesize($size) {
if($size)
$this->maxfilesize = $size * 1024 * 1024;
}
/**
* Set action to do when the max file size exceeds
*
* @param string $action
*/
function set_exceeded_size($action) {
switch($action) {
case 'delete':
case 'remove': $this->exceededsize = 'delete';
break;
case 'rename':
default: $this->exceededsize = 'rename';
break;
}
}
/**
* Set default timezone
*
* @param string $timezone
*/
function set_default_timezone($timezone = NULL) {
if($timezone) $this->default_timezone = $timezone;
date_default_timezone_set($this->default_timezone);
}
/**
* Get the log filename
*
* @return string $filename
*/
function get_filename() {
return $this->filename;
}
/**
* Get the log directory
*
* @return string $directory
*/
function get_directory() {
return $this->directory;
}
/**
* Log method
*
* @param string $string
* @param string $log_type
*/
function log( $string, $log_type = "I" ) {
$datetime = $this->datetime();
$fwrite = $datetime . " " . $this->log_types[$log_type] . " ". $string . "\r\n";
fwrite($this->handle, $fwrite) or die('Error while trying to write on file specified.');
}
/**
* Alias for debugging log
*
* @param string $string
*/
function _($string) {
$this->log($string, 'D');
}
/**
* Debug method
*
* @param string $string
*/
function debug($string) {
$this->log($string, 'D');
}
/**
* Info method
*
* @param string $string
*/
function info($string) {
$this->log($string, 'I');
}
/**
* Warning method
*
* @param string $string
*/
function warn($string) {
$this->log($string, 'W');
}
/**
* Error method
*
* @param string $string
*/
function error($string) {
$this->log($string, 'E');
}
/**
* Profile method
*
* The method must be called two times. The first call saves the timestamp on
* $start_profile; the second call returns the execution time of script.
*
* @param string $string
*/
function profile($string) {
if(!$this->start_profile) {
$this->start_profile = microtime(true);
$this->log($string,'P');
} else {
$execution_time = microtime(true) - $this->start_profile;
$this->log($string.$execution_time,'P');
unset($this->start_profile);
unset($this->$execution_time);
}
}
}
?>