-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.php
More file actions
294 lines (232 loc) · 8.17 KB
/
Copy pathLogger.php
File metadata and controls
294 lines (232 loc) · 8.17 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
<?
public class Logger {
/**
* Incremental log, where each entry is an array with the following elements:
*
* - timestamp => timestamp in seconds as returned by time()
* - level => severity of the bug; one between debug, warning, error, critical
* - name => name of the log entry, optional
* - message => actual log message
*/
protected static $log = [];
/**
* Whether to print log entries to screen as they are added.
*/
public static $print_log = true;
/**
* Whether to write log entries to file as they are added.
*/
public static $write_log = false;
/**
* Directory where the log will be dumped, without final slash; default
* is this file's directory
*/
public static $log_dir = __DIR__;
/**
* File name for the log saved in the log dir
*/
public static $log_file_name = "log";
/**
* File extension for the logs saved in the log dir
*/
public static $log_file_extension = "log";
/**
* Whether to append to the log file (true) or to overwrite it (false)
*/
public static $log_file_append = true;
/**
* Absolute path of the log file, built at run time
*/
private static $log_file_path = '';
/**
* Where should we write/print the output to? Built at run time
*/
private static $output_streams = [];
/**
* Whether the init() function has already been called
*/
private static $logger_ready = false;
/**
* Associative array used as a buffer to keep track of timed logs
*/
private static $time_tracking = [];
/**
* Add a log entry with an informational message for the user.
*/
public static function info( $message, $name = '' ) {
return self::add( $message, $name, 'info' );
}
/**
* Add a log entry with a diagnostic message for the developer.
*/
public static function debug( $message, $name = '' ) {
return self::add( $message, $name, 'debug' );
}
/**
* Add a log entry with a warning message.
*/
public static function warning( $message, $name = '' ) {
return self::add( $message, $name, 'warning' );
}
/**
* Add a log entry with an error - usually followed by
* script termination.
*/
public static function error( $message, $name = '' ) {
return self::add( $message, $name, 'error' );
}
/**
* Start counting time, using $name as identifier.
*
* Returns the start time or false if a time tracker with the same name
* exists
*/
public static function time( string $name ) {
if ( ! isset( self::$time_tracking[ $name ] ) ) {
self::$time_tracking[ $name ] = microtime( true );
return self::$time_tracking[ $name ];
}
else {
return false;
}
}
/**
* Stop counting time, and create a log entry reporting the elapsed amount of
* time.
*
* Returns the total time elapsed for the given time-tracker, or false if the
* time tracker is not found.
*/
public static function timeEnd( string $name ) {
if ( isset( self::$time_tracking[ $name ] ) ) {
$start = self::$time_tracking[ $name ];
$end = microtime( true );
$elapsed_time = number_format( ( $end - $start), 2 );
unset( self::$time_tracking[ $name] );
self::add( "$elapsed_time seconds", "'$name' took", "timing" );
return $elapsed_time;
}
else {
return false;
}
}
/**
* Add an entry to the log.
*
* This function does not update the pretty log.
*/
private static function add( $message, $name = '', $level = 'debug' ) {
/* Create the log entry */
$log_entry = [
'timestamp' => time(),
'name' => $name,
'message' => $message,
'level' => $level,
];
/* Add the log entry to the incremental log */
self::$log[] = $log_entry;
/* Initialize the logger if it hasn't been done already */
if ( ! self::$logger_ready ) {
self::init();
}
/* Write the log to output, if requested */
if ( self::$logger_ready && count( self::$output_streams ) > 0 ) {
$output_line = self::format_log_entry( $log_entry ) . PHP_EOL;
foreach ( self::$output_streams as $key => $stream ) {
fputs( $stream, $output_line );
}
}
return $log_entry;
}
/**
* Take one log entry and return a one-line human readable string
*/
public static function format_log_entry(array $log_entry )
{
$log_line = "";
if ( ! empty( $log_entry ) ) {
/* Make sure the log entry is stringified */
$log_entry = array_map( function( $v ) { return print_r( $v, true ); }, $log_entry );
/* Build a line of the pretty log */
$log_line .= date( 'c', $log_entry['timestamp'] ) . " ";
$log_line .= "[" . strtoupper( $log_entry['level'] ) . "] : ";
if ( ! empty( $log_entry['name'] ) ) {
$log_line .= $log_entry['name'] . " => ";
}
$log_line .= $log_entry['message'];
}
return $log_line;
}
/**
* Determine whether an where the log needs to be written; executed only
* once.
*
* @return {array} - An associative array with the output streams. The
* keys are 'output' for STDOUT and the filename for file streams.
*/
public static function init() {
if ( ! self::$logger_ready ) {
/* Print to screen */
if ( true === self::$print_log ) {
self::$output_streams[ 'stdout' ] = STDOUT;
}
/* Build log file path */
if ( file_exists( self::$log_dir ) ) {
self::$log_file_path = implode( DIRECTORY_SEPARATOR, [ self::$log_dir, self::$log_file_name ] );
if ( ! empty( self::$log_file_extension ) ) {
self::$log_file_path .= "." . self::$log_file_extension;
}
}
/* Print to log file */
if ( true === self::$write_log ) {
if ( file_exists( self::$log_dir ) ) {
$mode = self::$log_file_append ? "a" : "w";
self::$output_streams[ self::$log_file_path ] = fopen ( self::$log_file_path, $mode );
}
}
}
/* Now that we have assigned the output stream, this function does not need
to be called anymore */
self::$logger_ready = true;
}
/**
* Dump the whole log to the given file.
*
* Useful if you don't know before-hand the name of the log file. Otherwise,
* you should use the real-time logging option, that is, the $write_log or
* $print_log options.
*
* The method format_log_entry() is used to format the log.
*
* @param {string} $file_path - Absolute path of the output file. If empty,
* will use the class property $log_file_path.
*/
public static function dump_to_file( $file_path='' ) {
if ( ! $file_path ) {
$file_path = self::$log_file_path;
}
if ( file_exists( dirname( $file_path ) ) ) {
$mode = self::$log_file_append ? "a" : "w";
$output_file = fopen( $file_path, $mode );
foreach ( self::$log as $log_entry ) {
$log_line = self::format_log_entry( $log_entry );
fwrite( $output_file, $log_line . PHP_EOL );
}
fclose( $output_file );
}
}
/**
* Dump the whole log to string, and return it.
*
* The method format_log_entry() is used to format the log.
*/
public static function dump_to_string() {
$output = '';
foreach ( self::$log as $log_entry ) {
$log_line = self::format_log_entry( $log_entry );
$output .= $log_line . PHP_EOL;
}
return $output;
}
}
?>