-
Notifications
You must be signed in to change notification settings - Fork 1
/
Amslib_Log.php
345 lines (267 loc) · 8.69 KB
/
Amslib_Log.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/*******************************************************************************
* Copyright (c) {15/03/2008} {Christopher Thomas}
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contributors/Author:
* {Christopher Thomas} - Creator - [email protected]
*
*******************************************************************************/
/**
* class: Amslib_Log
*
* group: core
*
* file: Amslib_Log.php
*
* description: TODO
*/
class Amslib_Log extends Amslib_Mixin
{
protected $log4php;
protected $appender;
protected $appenderMap = array(
"file" => "LoggerAppenderFile"
);
static protected $defaultLogger = "error_log";
protected function createFileLog(&$filename)
{
$dirname = dirname($filename);
$basename = Amslib_String::slugify2(basename($filename),"-",".");
$filename = "$dirname/$basename";
if(file_exists($filename)) return true;
if(!is_dir(dirname($filename)) && !@mkdir(dirname($filename),0777,true)){
Amslib_Debug::log(error_get_last(),$dirname,$basename,$filename);
return false;
}
$t=$c=0;
if($t=@touch($filename) && $c=@chmod($filename,0777)){
return true;
}
Amslib_Debug::log("file failed to create, or modify it's permissions",error_get_last(),"touch=".intval($t),"chmod=".intval($c));
return false;
}
// FIXME: this method no longer works, it's supposed to obtain the function call which was the first entry point
// into the logging system, so we can record the moment the logging system started, we can report where
// in the code the person should look for the origin of these logging requests, but since I changed
// around a lot the logging system, the assumptions and code it uses is now out of date :(
static protected function getLogOrigin()
{
$stack = Amslib_Debug::getStackTrace();
if(!is_numeric($function)) $function = count($stack) > 1 ? 1 : 0;
$line = array("line"=>-1);
$function = false;
if($function == 0){
if(isset($stack[$function])){
$line = $stack[$function]["line"];
$f = explode("/",$stack[$function]["file"]);
$function = array(
"class"=>"",
"type"=>"",
"function"=>end($f)
);
}
}else{
if(isset($stack[$function-1])){
$line = $stack[$function-1]["line"];
}
if(isset($stack[$function])){
$function = $stack[$function];
}
}
if(!$function || !isset($function["class"]) || !isset($function["type"]) || !isset($function["function"])){
$function = "(ERROR, function invalid: ".Amslib_Debug::dump($function).")";
$data = array(Amslib_Debug::dump($stack));
}else{
$function = "{$function["class"]}{$function["type"]}{$function["function"]}($line)";
//$data[] = Amslib_Debug::dump($stack);
}
}
static protected function processLog($type,$vargs)
{
if(!in_array($type,array("trace","message","debug","info","warn","error","fatal"))) return false;
if(count($vargs) == 0) return false;
if(is_string($vargs[0]) && strlen($vargs[0])){
$logger = self::getInstance($vargs[0]);
if($logger) array_shift($vargs);
}
if(!$logger){
$logger = self::getInstance(self::getDefaultLogger());
}
$message = self::processMessage($vargs);
foreach($message as $m){
$logger->log4php->$type($m);
}
}
static protected function processMessage($list)
{
$data = array();
$function = false;
foreach($list as $k=>$a){
if(is_string($a) && strpos($a,"stack_trace") === 0){
$command = explode(",",$a);
$stack = Amslib_Debug::getStackTrace("type","text");
$stack = explode("\n",$stack);
$c = count($command);
if($c == 2){
$stack = array_slice($stack,$command[1]);
}else if($c == 3 && $command[2] > 0){
$stack = array_slice($stack,$command[1],$command[2]);
}
$trace = array("\n");
foreach($stack as $row){
$trace[] = "[STACK TRACE] ".str_replace("\n","",Amslib_Debug::dump($row));
}
$data[] = implode("\n",$trace);
}else if(is_string($a) && strpos($a,"func_offset") === 0){
$command = explode(",",array_shift($list));
if(count($command) == 1) $function = $command[0];
}else{
if(is_object($a)) $a = array(get_class($a),Amslib_Debug::dump($a));
if(is_array($a)) $a = Amslib_Debug::dump($a);
if(is_bool($a)) $a = $a ? "true" : "false";
if(is_null($a)) $a = "null";
$a = trim(preg_replace("/\s+/"," ",$a));
$data[] = "arg[$k]=> $a";
}
}
$function = self::getLogOrigin();
return $data;
}
public function __construct($name=NULL)
{
$name = $this->setName($name);
$this->log4php = Logger::getLogger($name);
}
static public function &getInstance($name=NULL)
{
static $instance = array();
if($name === NULL){
$keys = array_keys($instance);
$name = array_shift($keys);
}
if(!strlen($name)) $name = self::getDefaultLogger();
if(!isset($instance[$name])){
$object = new self($name);
$name = $object->getName();
// If the instance already exists, we throw away the newly created one
// we can't safely do this before we create the object because it might be invalid and the
// test code for this is inside the class, so in this scenario, we just take a small hit
if(!isset($instance[$name])) $instance[$name] = $object;
if(!self::getDefaultLogger()){
self::setDefaultLogger($name);
}
}
return $instance[$name];
}
public function setName($name)
{
if(!$name || !is_string($name) || !strlen($name)) $name = __CLASS__;
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function createAppender($type,$name)
{
if(!isset($this->appenderMap[$type])) return false;
$appender = new $this->appenderMap[$type]($name);
$appender->setThreshold("all");
$this->appender[] = $appender;
return count($this->appender)-1;
}
public function setDestination($index, $destination,$append=true)
{
if(!isset($this->appender[$index])) return false;
switch(get_class($this->appender[$index])){
case "LoggerAppenderFile":{
$exists = $this->createFileLog($destination);
if($exists){
$this->appender[$index]->setFile($destination);
$this->appender[$index]->setAppend($append);
// Create a default layout, which you can override later
$layout = new LoggerLayoutTTCC();
$layout->activateOptions();
$this->appender[$index]->setLayout($layout);
}else{
// We do this to make sure the appender cannot be activated or used
unset($this->appender[$index]);
// Ironically, the logging class fails, so we use the error log :)
Amslib_Debug::log(__METHOD__.", Failed to find or create the log file that we needed");
}
}break;
}
return true;
}
public function setThreshold($index,$threshold)
{
if(!isset($this->appender[$index])) return false;
$this->appender[$index]->setThreshold($threshold);
return true;
}
public function addAppender($index)
{
if(!isset($this->appender[$index])) return false;
$this->appender[$index]->activateOptions();
$this->log4php->addAppender($this->appender[$index]);
return true;
}
static public function setDefaultLogger($name)
{
self::$defaultLogger = $name;
}
static public function getDefaultLogger()
{
return self::$defaultLogger;
}
// NOTE: this method exists in the online docs, but not in the code,
// so I'm shadowing an "info" debug message for now
static public function trace($vargs)
{
$vargs = func_get_args();
self::processLog("info",$vargs);
}
static public function message($vargs)
{
$vargs = func_get_args();
self::processLog("info",$vargs);
}
static public function debug($vargs)
{
$vargs = func_get_args();
self::processLog("debug",$vargs);
}
static public function info($vargs)
{
$vargs = func_get_args();
self::processLog("info",$vargs);
}
static public function warn($vargs)
{
$vargs = func_get_args();
self::processLog("warn",$vargs);
}
static public function error($vargs)
{
$vargs = func_get_args();
self::processLog("error",$vargs);
}
static public function fatal($vargs)
{
$vargs = func_get_args();
self::processLog("fatal",$vargs);
}
}