-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloggit.php
89 lines (80 loc) · 2.55 KB
/
floggit.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
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
<?php
/*
Plugin Name: Floggit
Plugin URI: https://www.designerandgeek.com
Description: Easier logging to debug.log.
Version: 1.1
Author: Jørn Støylen
Author URI: https://www.designerandgeek.com
*/
namespace PrikkPrikkPrikk {
class FloggitClass
{
// Have we already emptied the log file?
private static $flogged = false;
/**
* Easier logging to debug.log.
*
* Just a wrapper for error_log that pretty prints arrays and objects
* and includes the filename and line number where the logging happened.
*
* Accepts multiple parameters, prints one line for each parameter.
*
* @param ...$logs One or more things to log.
*/
public static function loggit(...$logs)
{
// Don't log if logging is off
if (WP_DEBUG_LOG==false) {
return;
}
$backtrace = debug_backtrace();
$level = 0;
while (basename(__FILE__)==basename($backtrace[$level]['file'])) {
$level++;
}
$backtrace = debug_backtrace()[$level];
$bt_file = basename($backtrace['file']);
$bt_line = $backtrace['line'];
$bt_string = "[$bt_file:$bt_line] ";
foreach ($logs as $log) {
if (is_array($log) || is_object($log)) {
error_log($bt_string . PHP_EOL . print_r($log, true));
} else {
error_log($bt_string . $log);
}
}
}
/**
* Fresh loggit: Empty debug.log first, then loggit().
*
* Since this is an extra step in the call stack, set backtrace level to 2.
*
* @param ...$logs One or more things to log.
*/
public static function floggit(...$logs)
{
// Don't log if logging is off
if (WP_DEBUG_LOG==false) { return; }
// Static class variable flag keeps track of whether debug.log has been purged or not
if (self::$flogged==false) {
$logfile = (is_bool(WP_DEBUG_LOG)) ? (WP_CONTENT_DIR . '/debug.log') : WP_DEBUG_LOG;
if(file_exists($logfile)) {
unlink($logfile);
}
self::$flogged = true;
}
loggit(...$logs);
}
}
}
namespace {
function loggit(...$logs)
{
return \PrikkPrikkPrikk\FloggitClass::loggit(...$logs);
}
function floggit(...$logs)
{
return \PrikkPrikkPrikk\FloggitClass::floggit(...$logs);
}
}