-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLogger.php
85 lines (67 loc) · 1.88 KB
/
Logger.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
<?php
/**
* 日志操作
* Created by thenbsp ([email protected])
*/
class Logger
{
/**
* 日志文件路径
*/
public $savepath;
/**
* 构造方法
*/
public function __construct($savepath)
{
if( NULL === $savepath ) {
throw new Exception('Log save path is required');
}
if( ! is_writable($savepath) ) {
throw new Exception('Log save path unwritable');
}
$this->savepath = realpath($savepath) . DIRECTORY_SEPARATOR;
}
/**
* 调试日志
*/
public function debug($message)
{
return $this->_set('info', $message, debug_backtrace());
}
/**
* 错误日志
*/
public function error($message)
{
return $this->_set('error', $message, debug_backtrace());
}
/**
* 警告日志
*/
public function warning($message)
{
return $this->_set('warning', $message, debug_backtrace());
}
/**
* 写入日志方法
*/
private function _set($level = 'debug', $message, $debug) {
$level = strtolower($level);
$level = in_array($level, array('debug', 'error', 'warning')) ? $level : 'debug';
$filename = date('Y-m-d');
$fullname = $this->savepath."{$filename}-{$level}.php";
if( ! $fp = fopen($fullname, 'ab') ) {
return FALSE;
}
$file = isset($debug[0]['file']) ? $debug[0]['file'] : '';
$line = isset($debug[0]['line']) ? $debug[0]['line'] : '';
$message = "Created: ".date('Y-m-d H:i')." {$file}:{$line}".PHP_EOL."Message: {$message}".PHP_EOL.PHP_EOL;
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
chmod($fullname, '0666');
return TRUE;
}
}