-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog.php
61 lines (50 loc) · 1.44 KB
/
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
<?php
/*
+----------------------------------------------------------------------+
| Author: Xingzhi Liu <[email protected]> |
+----------------------------------------------------------------------+
*/
class Gek_Log {
private static $levels = array(
'NONE' => 0,
'FATAL' => 1,
'ERROR' => 2,
'WARN' => 3,
'INFO' => 4,
'DEBUG' => 5,
'ALL' => 6
);
private static $logpath = '';
private static $level = 'DEBUG';
public static function init($logpath, $level='')
{
self::$logpath = $logpath;
if(!empty($level) ) {
$level = strtoupper($level);
if(isset(self::$levels[$level])) {
self::$level = $level;
}
}
}
public static function write($msg, $level='ERROR')
{
if(empty(self::$logpath)) {
return false;
}
$level = strtoupper($level);
if(!isset(self::$levels[$level]) || (self::$levels[$level] > self::$levels[self::$level])) {
return false;
}
if(!$fp = @fopen(self::$logpath, 'ab')) {
return false;
}
$message = '';
$message .= $level.' '.(($level == 'INFO' || $level == 'WARN') ? ' -' : '-').' '.date('Y-m-d H:i:s'). ' --> '.$msg."\n";
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
@chmod(self::$logpath, 0666);
return false;
}
}