-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.php
59 lines (46 loc) · 1.44 KB
/
logging.php
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
<?php
namespace SmartHistoryTourManager;
class Logging {
const DEBUG_LOG_FILE = '/tmp/wp-shtm-debug.log';
const TO_FILE = 0;
const TO_STDOUT = 1;
private static $output = self::TO_FILE;
public static function set_output($output) {
self::$output = $output;
}
public static function debug_log($msg, $prefix = null) {
if(is_null($prefix)) {
$prefix = "DEBUG";
}
$msg = self::ensure_string_description($msg);
$msg = "$prefix: $msg" . PHP_EOL;
if(self::$output == self::TO_STDOUT) {
echo $msg;
} else {
self::log_to_file($msg, self::DEBUG_LOG_FILE);
}
}
private static function ensure_string_description($input) {
if(!is_string($input)) {
return var_export($input, true);
}
return $input;
}
private static function log_to_file($msg, $path) {
file_put_contents($path, $msg, FILE_APPEND | LOCK_EX);
}
}
// convenience function to call the debug log withot Logging class
function debug_log($msg, $prefix = null) {
Logging::debug_log($msg, $prefix);
}
// this can be hooked into wordpress' wpdb query to log all db queries
function debug_log_query($query) {
// remove excessive whitespace and log
$q = preg_replace(
array('/^\s+/', '/\s+$/', '/\s+/'), array('', '', ' '), $query);
debug_log($q, "QUERY");
// return query unchanged
return $query;
}
?>