forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLog.php
More file actions
348 lines (299 loc) · 8.43 KB
/
SLog.php
File metadata and controls
348 lines (299 loc) · 8.43 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
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
346
347
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
*/
class SLog
{
public static $CONSOLE = true;
public static $LOG = false;
public static $LOGFILE = "";
// Log level definition
const LOG_LEVEL_NONE = 0x00;
const LOG_LEVEL_FATAL = 0x01;
const LOG_LEVEL_WARNING = 0x02;
const LOG_LEVEL_NOTICE = 0x04;
const LOG_LEVEL_TRACE = 0x08;
const LOG_LEVEL_DEBUG = 0x10;
const LOG_LEVEL_ALL = 0xFF;
// Log type definition
const LOG_TYPE_LOCAL_LOG = 'LOCAL_LOG';
const LOG_TYPE_NET_LOG = 'NET_LOG';
/**
* @var array
*/
public static $logLevelMap = array(
self::LOG_LEVEL_NONE => 'NONE',
self::LOG_LEVEL_FATAL => 'FATAL',
self::LOG_LEVEL_WARNING => 'WARNING',
self::LOG_LEVEL_NOTICE => 'NOTICE',
self::LOG_LEVEL_TRACE => 'TRACE',
self::LOG_LEVEL_DEBUG => 'DEBUG',
self::LOG_LEVEL_ALL => 'ALL',
);
/**
* @var array
*/
public static $logTypes = array(
self::LOG_TYPE_LOCAL_LOG,
self::LOG_TYPE_NET_LOG,
);
/**
* Log output device type, can be "LOCAL_LOG", "STDOUT"
*
* @var string
*/
protected $type;
/**
* Log level
*
* @var int
*/
protected $level;
/**
* Log file path for local log file, or module name for comLog
*
* @var string
*/
protected $path;
/**
* Log file name
*
* @var string
*/
protected $filename;
/**
* Client IP
*
* @var string
*/
protected $clientIP;
/**
* Log Id for current request
*
* @var uint
*/
protected $logId;
/**
* PHP start time of current request
*
* @var uint
*/
protected $startTime;
/**
* @var log
*/
private static $instance = null;
/**
* Constructor
*
* @param array $conf
* @param uint $startTime
*/
private function __construct($conf, $startTime)
{
$this->type = $conf->type;
$this->level = $conf->level;
$this->path = $conf->path;
$this->filename = $conf->filename;
$this->startTime = $startTime;
$this->logId = $this->__logId();
$this->clientIP = SUtil::getIP();
if ($this->type === self::LOG_TYPE_NET_LOG) {
openlog($conf['appName'], LOG_PID | LOG_PERROR, LOG_LOCAL1);
}
}
/**
* @return log
*/
public static function getInstance()
{
if (self::$instance === null) {
$startTime = microtime(true) * 1000;
$logConf = !empty($GLOBALS['LOG_CONF']) ? $GLOBALS['LOG_CONF'] : 'default';
$logConfig = SConfig::getConfig(ROOT_CONFIG."/log.conf", $logConf);
if (!empty($logConfig->filename)) {
$logConfig->filename = sprintf($logConfig->filename, date('Ymd'));
self::$instance = new SLog($logConfig, $startTime);
} else {
$stdClass = new stdClass();
$stdClass->appName = 'AllLog';
$stdClass->type = 'LOCAL_LOG';
$stdClass->level = '0x15';
$stdClass->path = '/tmp';
$stdClass->filename = 'All_log.'.date('Ymd');
self::$instance = new SLog($stdClass, $startTime);
}
}
return self::$instance;
}
/**
* Write debug log
*
* @return int
*/
public static function debug()
{
$args = func_get_args();
return SLog::getInstance()->writeLog(self::LOG_LEVEL_DEBUG, $args);
}
/**
* Write trace log
*
* @return int
*/
public static function trace()
{
$args = func_get_args();
return SLog::getInstance()->writeLog(self::LOG_LEVEL_TRACE, $args);
}
/**
* Write notice log
*
* @return int
*/
public static function notice()
{
$args = func_get_args();
return SLog::getInstance()->writeLog(self::LOG_LEVEL_NOTICE, $args);
}
/**
* Write warning log
*
* @return int
*/
public static function warning()
{
$args = func_get_args();
return SLog::getInstance()->writeLog(self::LOG_LEVEL_WARNING, $args);
}
/**
* Write fatal log
*
* @return int
*/
public static function fatal()
{
$args = func_get_args();
return SLog::getInstance()->writeLog(self::LOG_LEVEL_FATAL, $args);
}
/**
* Get logId for current http request
*
* @return int
*/
public static function logId()
{
return SLog::getInstance()->logId;
}
/**
* @param $logId
*/
public static function setLogId($logId)
{
SLog::getInstance()->logId = $logId;
}
/**
* Get the real remote client's IP
*
* @return string
*/
public static function getClientIP()
{
return SLog::getInstance()->clientIP;
}
/**
* Write log
*
* @param int $level Log level
* @param array $args format string and parameters
*
* @return int
*/
protected function writeLog($level, Array $args)
{
if ($level > $this->level || !isset(self::$logLevelMap[$level])) {
return 0;
}
$timeUsed = microtime(true) * 1000 - $this->startTime;
$fmt = array_shift($args);
$str = vsprintf($fmt, $args);
if ($level == self::LOG_LEVEL_NOTICE) {
$str = sprintf(
"%s:@@%s@@host[%s]@@ip[%s]@@logId[%u]@@uri[%s]@@time_used[%d]@@%s\n",
self::$logLevelMap[$level],
date('Y-m-d H:i:s:', time()),
$_SERVER['HTTP_HOST'],
$this->clientIP,
$this->logId,
isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
$timeUsed, $str
);
} else {
$str = sprintf(
"%s: %s host[%s] ip[%s] logId[%u] uri[%s] time_used[%d] %s\n",
self::$logLevelMap[$level],
date('Y-m-d H:i:s:', time()),
$_SERVER['HTTP_HOST'],
$this->clientIP,
$this->logId,
isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
$timeUsed, $str
);
}
if ($this->type === self::LOG_TYPE_LOCAL_LOG) {
$filename = $this->path.'/'.$this->filename;
if ($level < self::LOG_LEVEL_NOTICE) {
$filename .= '.wf';
}
$strLen = file_put_contents($filename, $str, FILE_APPEND | LOCK_EX);
@chmod($filename, 0777);
return $strLen;
} else {
syslog(LOG_DEBUG, $str);
return strlen($str);
}
}
/**
* @return int
*/
private function __logId()
{
$arr = gettimeofday();
return ((($arr['sec'] * 100000 + $arr['usec'] / 10) & 0x7FFFFFFF));
}
/**
* @param $info
*/
public static function write($info)
{
if (is_object($info) || is_array($info)) {
$infoText = var_export($info, true);
} elseif (is_bool($info)) {
$infoText = $info ? "true" : "false";
} else {
$infoText = $info;
}
$infoText = "[".date("Y-m-d H:i:s")."] ".$infoText;
if (!empty(SLog::$LOGFILE)) {
error_log($infoText."\r\n", 3, SLog::$LOGFILE);
} else {
error_log($infoText);
}
if (SLog::$CONSOLE)
echo "<!--\n".$infoText."\n-->";
}
}