-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.class.php
More file actions
151 lines (139 loc) · 3.79 KB
/
Log.class.php
File metadata and controls
151 lines (139 loc) · 3.79 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
<?php
/**
* Generic static logging class with 3 levels of logging.
* - The file and line number of where the log was thrown are automatically added to the log item
* - Optionally you may pass a "context", which can be any string and is supposed to give a clue to where the sollution can be found
*
* Log it:
* Log::info($msg)
* Log::warn($msg[, $context])
* Log::error($msg[, $context])
*
* Do we have any logs? Request can be filtered by type/level:
* Log::count(['info'|'warn'|'error'])
*
* Get a raw array or output directly to screen. Can be filtered by type/level:
* Log::retrieve()
* Log::output()
*
* Clearing the log (can also be filtered):
* Log::clear()
*/
class Log {
private static $logs = array();
/**
* Normal log
* @param type $msg
* @param mixed [optional] $ctx Additional context info (see addLog for details)
*/
public static function info($msg, $ctx = '') {
self::addLog('info', $msg, $ctx);
}
/**
* Log a warning
* @param string $msg
* @param mixed [optional] $ctx Context where user can fix this issue (see addLog for details)
*/
public static function warn($msg, $ctx = '') {
self::addLog('warn', $msg, $ctx);
}
/**
* Log an error
* @param string $msg
* @param mixed [optional] $ctx Context where user can fix this issue (see addLog for details)
*/
public static function error($msg, $ctx = '') {
self::addLog('error', $msg, $ctx);
}
/**
* Severe logs get more information so they can be debugged.
* @param string $severity
* @param string $msg
* @param mixed $ctx Context of log item. If a number, the debug_backtrace will go
* further back in history. If a string, it will be added as such
* to the log array
*/
private static function addLog($severity, $msg, $ctx) {
$debug = debug_backtrace(); // Gives info from where the log is thrown
$history = is_int($ctx)? 1 + $ctx : 1; // 1 displays info caller of warn() and/or error()
$i = count(self::$logs);
self::$logs[$i] = array(
'severity' => $severity,
'message' => $msg,
'in_file' => $debug[$history]['file'],
'at_line' => $debug[$history]['line']
);
// Set context if not specified -uses classname if available, defaults to filename
if ((!$ctx || is_int($ctx)) && isset($debug[0]['class'])) {
$ctx = $debug[0]['class'];
}
// If context is a string, we'll append it to the log
if ($ctx && is_string($ctx)) {
self::$logs[$i]['context'] = $ctx;
}
}
/**
* Check if a log is made for given type (or any)
* @param string $type
* @return boolean
*/
public static function count($type = '') {
$total = count(self::$logs);
if ($type === '' && $total > 0) {
return $total;
}
$i = 0;
foreach (self::$logs as $log) {
if ($log['severity'] === $type) {
$i++;
}
}
return $i;
}
/**
* Retrieve raw array of log for given type (or any)
* @param string $type
* @return array
*/
public static function retrieve($type = '') {
if ($type) {
$typelogs = array();
foreach (self::$logs as $log) {
if ($log['severity'] === $type) {
$typelogs[] = $log;
}
}
return $typelogs;
} else {
return self::$logs;
}
}
/**
* Output log for given type (or any) to screen
* @TODO Make this look nice
* @param string $type
*/
public static function output($type = '') {
echo '<pre>';
print_r(self::retrieve($type));
echo '</pre>';
}
/**
* Clear requested logs. If type filter is given, the relative order of
* logs is preserved
* @param string $type
*/
public static function clear($type = '') {
if ($type) {
$l = count(self::$logs);
for ($i = 0; $i < $l; $i++) {
if (self::$logs[$i]['severity'] === $type) {
unset(self::$logs[$i]);
}
}
self::$logs = array_values(self::$logs);
} else {
self::$logs = array();
}
}
}