-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLog.php
170 lines (154 loc) · 4.21 KB
/
FileLog.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Yonna\Log;
use Throwable;
class FileLog
{
private $root = null;
public function __construct()
{
if (Config::getDir() === null) {
trigger_error('Set Dir for FileLog.');
return;
}
$this->root = realpath(Config::getDir());
}
/**
* 递归删除过期日志
* @param $dir
* @param integer $timestamp 删除这一天之前的
*/
private function dirExpire($dir, $timestamp)
{
if (!is_dir($dir)) {
return;
}
$files = opendir($dir);
while (false !== ($file = readdir($files))) {
if ($file != '.' && $file != '..') {
$t = strtotime($file);
if ($t > $timestamp) {
continue;
}
$realDir = realpath($dir);
$realFile = $realDir . DIRECTORY_SEPARATOR . $file;
if (is_dir($realFile)) {
static::dirExpire($realFile, $timestamp);
@rmdir($realFile);
} else {
@unlink($realFile);
}
}
}
closedir($files);
if ($dir !== $this->root . DIRECTORY_SEPARATOR . Config::getFile()) {
@rmdir($dir);
}
}
/**
* 清除文件日志
*/
private function clear()
{
if (Config::getFileExpireDay() <= 0) {
return;
}
$this->dirExpire($this->root . DIRECTORY_SEPARATOR . Config::getFile(), time() - 86400 * Config::getFileExpireDay());
}
/**
* 获取日志目录,以天分割
* @param $key
* @return string
*/
private function dir($key)
{
$path = $this->root
. DIRECTORY_SEPARATOR . Config::getFile()
. DIRECTORY_SEPARATOR . date('Y-m-d')
. DIRECTORY_SEPARATOR . $key;
$temp = str_replace('\\', '/', $path);
$p = explode('/', $temp);
$tempLen = count($p);
$temp = '';
for ($i = 0; $i < $tempLen; $i++) {
$temp .= $p[$i] . DIRECTORY_SEPARATOR;
if (!is_dir($temp)) {
@mkdir($temp);
@chmod($temp, 0644);
}
}
$temp = realpath($temp) . DIRECTORY_SEPARATOR;
return $temp ? $temp : false;
}
/**
* 获取日志文件名
* @param $type
* @return string
*/
private function file($type)
{
return strtolower($type) . '.log';
}
/**
* 写入日志
* @param $type
* @param array $data
* @param string $key
*/
private function append($type, $key, array $data = [])
{
if (empty($key) or empty($data)) {
return;
}
$append = '[time:' . date("Y-m-d H:i:s D T") . ']' . PHP_EOL;
if ($data) {
foreach ($data as $k => $v) {
if (is_array($v) || is_object($v)) {
$v = json_encode($v, JSON_UNESCAPED_UNICODE);
} else {
$v = (string)$v;
}
$data && $append .= " #{$k} " . $v . PHP_EOL;
}
}
@file_put_contents($this->dir($key) . $this->file($type), $append . PHP_EOL, FILE_APPEND);
$this->clear();
}
/**
* @param string $key
* @param Throwable $t
*/
public function throwable(Throwable $t, $key = 'default')
{
$this->append(Type::THROWABLE, $key, [
'code' => $t->getCode(),
'message' => $t->getMessage(),
'file' => $t->getFile(),
'line' => $t->getLine(),
'trace' => $t->getTrace(),
]);
}
/**
* @param array $data
* @param string $key
*/
public function info(array $data = [], $key = 'default')
{
$this->append(Type::INFO, $key, $data);
}
/**
* @param array $data
* @param string $key
*/
public function warning(array $data = [], $key = 'default')
{
$this->append(Type::WARNING, $key, $data);
}
/**
* @param array $data
* @param string $key
*/
public function error(array $data = [], $key = 'default')
{
$this->append(Type::ERROR, $key, $data);
}
}