-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogger.class.php
More file actions
233 lines (179 loc) · 7.28 KB
/
Logger.class.php
File metadata and controls
233 lines (179 loc) · 7.28 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
<?php
/**
*
* *******************************************************************
*
* Copyright (C) 2011 by Ad Astra Systems, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* *******************************************************************
*
* Logging class, use for all trace commands
*
*/
class Logger {
public static $DEBUG = 0;
public static $INFO = 1;
public static $WARNING = 2;
public static $ERROR = 3;
public static $FATAL = 4;
private static $debugLevel = 0;
private static $echoLog = false;
// //////////////////////////////////////////////////////////////////////////////////////
/**
* Class constructor
*/
public function __construct(){
self::init();
}
// //////////////////////////////////////////////////////////////////////////////////////
public static function echoLog(){
self::$echoLog = true;
}
// //////////////////////////////////////////////////////////////////////////////////////
/**
* Let the Logger catch any system errors
*/
public static function catchSysErrors(){
set_error_handler("Logger::sysErrorHandler");
}
// //////////////////////////////////////////////////////////////////////////////////////
/**
*
*/
private static function trace($level, $msg){
if ($level < self::$debugLevel){
return;
}
$bt = debug_backtrace();
$class = "";
$function = "";
// get class, function called by caller of caller of caller
if (isset($bt[2]['class'])){
$class = $bt[2]['class'];
$function = "." . $bt[2]['function'];
}
// get file, line where call to caller of caller was made
$file = $bt[1]['file'];
$line = $bt[1]['line'];
$file_name = basename($file);
if (self::$echoLog){
switch($level){
case self::$DEBUG: $levMsg = "<span style='color:009900'>debug</span>"; break;
case self::$INFO: $levMsg = "<span style='color:0000FF'>info</span>"; break;
case self::$WARNING:$levMsg = "<span style='color:FF6633'>warning</span>"; break;
case self::$ERROR: $levMsg = "<span style='color:FF0101'>error</span>"; break;
case self::$FATAL: $levMsg = "<span style='color:FF0000'><b>fatal</b></span>"; break;
}
$msg = "[$levMsg <span style='color:#000099'>$class$function</span>] <b>$msg</b>";
$msg .= "<span style='color: #444; font-style: italic;'> on line $line of $file_name</span>";
if (isset($bt[2]['file'])){
$fname = basename($bt[2]['file']);
$msg .= "<span style='color: #889; font-style: italic;'>, called from line ".$bt[2]['line']." of $fname</span>";
}
$msg .= "<br>\n";
echo $msg;
flush();
}
else {
// Get IP address....
// Translate level into text...
$levMsg = "????";
switch($level){
case self::$DEBUG: $levMsg = "DEBUG"; break;
case self::$INFO: $levMsg = "INFO"; break;
case self::$WARNING:$levMsg = "WARNING"; break;
case self::$ERROR: $levMsg = "ERROR"; break;
case self::$FATAL: $levMsg = "FATAL"; break;
}
$msg = "[$levMsg] $class$function $msg";
$msg .= " {on line $line of $file_name";
if (isset($bt[2]['file'])){
$fname = basename($bt[2]['file']);
$msg .= ", called from line ".$bt[2]['line']." of $fname";
}
if (isset($bt[3]['file'])){
$fname = basename($bt[3]['file']);
$msg .= ", called from line ".$bt[3]['line']." of $fname";
}
$msg .= "}";
error_log($msg);
}
}
// //////////////////////////////////////////////////////////////////////////////////////
/**
* Use to replace system error handler, if desired. Use Logger::catchSysErrors() to
* activate
*/
public static function sysErrorHandler($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
$levMsg = "SYS_ERROR";
break;
case E_USER_WARNING:
$levMsg = "SYS_WARNING";
break;
case E_USER_NOTICE:
$levMsg = "SYS_NOTICE";
break;
default:
$levMsg = "SYS_UNKNOWN";
break;
}
if (self::$echoLog){
switch($errno){
case E_USER_NOTICE: $levMsg = "<span style='color:0000FF'>info</span>"; break;
case E_USER_WARNING:$levMsg = "<span style='color:FF6633'>warning</span>"; break;
case E_USER_ERROR: $levMsg = "<span style='color:FF0101'>error</span>"; break;
default: $levMsg = "<span style='color:009900'>debug</span>"; break;
}
$msg = "[$levMsg <span style='color:#000099'>#$errno</span>] <b>$errstr</b>";
$msg .= "<span style='color: #444; font-style: italic;'> on line $errline of ".basename($errfile)."</span>";
$msg .= "<br>\n";
echo $msg;
flush();
}
else {
$msg = "[$levMsg] No: $errno Msg: $errstr";
$msg .= " {on line $errline of ".basename($errfile)."}";
error_log($msg);
}
/* Don't execute PHP internal error handler */
return true;
}
// //////////////////////////////////////////////////////////////////////////////////////
public static function setLevel($newLevel) { $debugLevel = $newLevel; }
public static function setLevelDebug() { self::$debugLevel = self::$DEBUG; }
public static function setLevelInfo() { self::$debugLevel = self::$INFO; }
public static function setLevelWarning() { self::$debugLevel = self::$WARNING; }
public static function setLevelError() { self::$debugLevel = self::$ERROR; }
public static function setLevelFatal() { self::$debugLevel = self::$FATAL; }
// //////////////////////////////////////////////////////////////////////////////////////
/** Print debug message, e.g debug(__CLASS__, 'some error message', __LINE__); */
public static function dump($var) { self::debug("<pre>" . print_r($var, true) . "</pre>"); }
public static function debug($msg){ self::trace(self::$DEBUG, $msg); }
public static function warning($msg){ self::trace(self::$WARNING, $msg); }
public static function warn($msg){ self::trace(self::$WARNING, $msg); }
public static function error($msg){ self::trace(self::$ERROR, $msg); }
public static function info($msg){ self::trace(self::$INFO, $msg); }
public static function fatal($msg){ self::trace(self::$FATAL, $msg); die(); }
// //////////////////////////////////////////////////////////////////////////////////////
}
?>