/
Log.php
152 lines (124 loc) · 3.66 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
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
<?php
namespace Li;
class Log
{
private static $_log;
private $_name = 'default';
private $_config = [];
private $_defaultConf = array(
'format' => 'YmdH', //按天分文件
'level' => [
'debug',
'warning',
'trace',
'fatal',
],
);
public static function log($name='default')
{
if (!(self::$_log instanceof self)) {
self::$_log = new self($name);
}
$trace = debug_backtrace();
self::$_log->file = $trace[0]['file'];
self::$_log->line = $trace[0]['line'];
self::$_log->_init($name);
return self::$_log;
}
private function _init($name=null)
{
if(empty($name)) {
$name = 'default';
}
$this->_name = $name;
if(isset(App::app()->config['log'][$name])) {
$this->_config[$name] = array_merge($this->_defaultConf, App::app()->config['log'][$name]);
} else {
$this->_config[$name] = $this->_defaultConf;
}
}
public function __construct($name=null)
{
$this->_config = App::app()->config['log'];
$this->_init($name);
}
/**
* @return string the log file name
*/
private function _getFileName($level)
{
$filename = 'log';
if($level == 'warning' || $level == 'fatal') {
$filename .= '.wf';
}
return $this->_name . '.' . $filename.'.'.date($this->_config[$this->_name]['format'], NOW);
}
/**
* 如果框架debug为true,返回true
* 如果框架debug为false,根据日志配置的debug返回
* if li global config debug is false,
* @return boolean if write return true, otherwise false
*/
private function _checkLevel($level)
{
if(in_array($level, $this->_config[$this->_name]['level'])) {
return true;
}
return false;
}
private function _write($message, $level)
{
if (!$this->_checkLevel($level)) {
return false;
}
$fileName = $this->_getFileName($level);
$filePath = PATH_APP.'runtime'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR;
if($message instanceof \Exception) {
$log = date('Y-m-d H:i:s', NOW).' '.'['.$level.'] '.$message->getMessage(). ' [' . $message->getFile().':'.$message->getLine() . "]\r\n";
} else {
$log = date('Y-m-d H:i:s', NOW).' '.'['.$level.'] '.$message. ' [' . $this->file.':'.$this->line . "]\r\n";
}
if(!is_dir($filePath)) {
mkdir($filePath);
}
if($message instanceof \Exception) {
$log = date('Y-m-d H:i:s', NOW).' '.'['.$level.'] '.$message->getMessage(). ' [' . $message->getFile().':'.$message->getLine() . "]\r\n";
} else {
$log = date('Y-m-d H:i:s', NOW).' '.'['.$level.'] '.$message. ' [' . $this->file.':'.$this->line . "]\r\n";
}
if(App::app()->config['debug']) {
echo $log . PHP_EOL;
} else {
error_log($log, 3, $filePath.$fileName);
}
$this->_configIndex=null;
}
/**
* trace日志内容
*/
public function debug($message)
{
$this->_write($message, __FUNCTION__);
}
/**
* trace日志内容
*/
public function trace($message)
{
$this->_write($message, __FUNCTION__);
}
/**
* error日志内容
*/
public function warning($message)
{
$this->_write($message, __FUNCTION__);
}
/**
* fatal日志内容
*/
public function fatal($message)
{
$this->_write($message, __FUNCTION__);
}
}