PhpLog.php
8.45 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
<?php
namespace WebPlugin;
/**
* 日志记录类
*
* @package WebPlugin
* @author Gtskk
* @copyright 2016/4/25 13:48 Gtskk<iamgtskk@gmail.com>
* @version: 0.0.1
*/
class PhpLog
{
const DEBUG = 1;// Most Verbose
const INFO = 2;// ...
const WARN = 3;// ...
const ERROR = 4;// ...
const FATAL = 5;// Least Verbose
const OFF = 6;// Nothing at all.
const LOG_OPEN = 1;
const OPEN_FAILED = 2;
const LOG_CLOSED = 3;
public $Log_Status = PhpLog::LOG_CLOSED;
public $DateFormat = "Y-m-d G:i:s";
public $MessageQueue;
private $filename;
private $log_file;
private $priority = PhpLog::INFO;
private $file_handle;
/**
* 日志记录类构造函数
*
* @param string $filepath 文件存储的路径
* @param string $timezone 时间格式,此处设置为"PRC"(中国)
* @param $priority
* 设置运行级别
*/
public function __construct($filepath, $timezone, $priority)
{
if ($priority == PhpLog::OFF) return;
$this->filename = date('Y-m-d', time()) . '.log'; //默认为以时间+.log的文件文件
$this->log_file = $this->createPath($filepath, $this->filename);
$this->MessageQueue = array();
$this->priority = $priority;
date_default_timezone_set($timezone);
if (!file_exists($filepath)) //判断文件路径是否存在
{
if (!empty($filepath)) //判断路径是否为空
{
if (!($this->_createDir($filepath))) {
die("创建目录失败!");
}
if (!is_writable($this->log_file)) {
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
return;
}
}
}
if ($this->file_handle = fopen($this->log_file, "a+")) {
$this->Log_Status = PhpLog::LOG_OPEN;
$this->MessageQueue[] = "The log file was opened successfully.";
} else {
$this->Log_Status = PhpLog::OPEN_FAILED;
$this->MessageQueue[] = "The file could not be opened. Check permissions.";
}
return;
}
public function __destruct()
{
if ($this->file_handle)
fclose($this->file_handle);
}
/**
*作用:创建目录
*输入:要创建的目录
*输出:true | false
*/
private function _createDir($dir)
{
return is_dir($dir) or (self::_createDir(dirname($dir)) and mkdir($dir, 0777));
}
/**
*作用:构建路径
*输入:文件的路径,要写入的文件名
*输出:构建好的路径字串
*/
private function createPath($dir, $filename)
{
if (empty($dir)) {
return $filename;
} else {
return $dir . "/" . $filename;
}
}
public function LogInfo($line)
{
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log($line, PhpLog::INFO, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogDebug($line)
{
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log($line, PhpLog::DEBUG, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogWarn($line)
{
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log($line, PhpLog::WARN, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogError($line)
{
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log($line, PhpLog::ERROR, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
public function LogFatal($line)
{
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
$this->Log($line, PhpLog::FATAL, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
}
/**
* Author : gu_yongkang
* Enter description here ...
* @param unknown_type $line
* content 内容
* @param unknown_type $priority
* 打印级别
* @param unknown_type $sFile
* 调用打印日志的文件名
* @param unknown_type $iLine
* 打印文件的位置(行数)
*/
public function Log($line, $priority, $sFile, $iLine)
{
if ($iLine > 0) {
// $line = iconv('GBK', 'UTF-8', $line);
if ($this->priority <= $priority) {
$status = $this->getTimeLine($priority, $sFile, $iLine);
$this->WriteFreeFormLine("$status $line \n");
}
} else {
/**
* AUTHOR : gu_yongkang
* 增加打印函数和文件名的功能
*/
$sAarray = array();
$sAarray = debug_backtrace();
$sGetFilePath = $sAarray[0]["file"];
$sGetFileLine = $sAarray[0]["line"];
if ($this->priority <= $priority) {
$status = $this->getTimeLine($priority, $sGetFilePath, $sGetFileLine);
unset($sAarray);
unset($sGetFilePath);
unset($sGetFileLine);
$this->WriteFreeFormLine("$status $line \n");
}
}
}
// 支持输入多个参数
public function WriteFreeFormLine($line)
{
if ($this->Log_Status == PhpLog::LOG_OPEN && $this->priority != PhpLog::OFF) {
if (fwrite($this->file_handle, $line) === false) {
$this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
}
}
}
private function getRemoteIP()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
if (!empty($ip)) {
return $ip;
}
}
}
}
return "_NO_IP";
}
private function getTimeLine($level, $FilePath, $FileLine)
{
$time = date($this->DateFormat);
$ip = $this->getRemoteIP();
switch ($level) {
case PhpLog::INFO:
return "$time, " . "INFO, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::WARN:
return "$time, " . "WARN, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::DEBUG:
return "$time, " . "DEBUG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::ERROR:
return "$time, " . "ERROR, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
case PhpLog::FATAL:
return "$time, " . "FATAL, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
default:
return "$time, " . "LOG, " . "$ip, " . "File[ $FilePath ], " . "Line[$FileLine]" . "------";
}
}
}