-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseLog.php
177 lines (162 loc) · 5.07 KB
/
DatabaseLog.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
171
172
173
174
175
176
177
<?php
namespace Yonna\Log;
use Exception;
use Throwable;
use Yonna\Database\DB;
use Yonna\Database\Driver\Mongo;
use Yonna\Database\Driver\Mysql;
use Yonna\Database\Driver\Pgsql;
class DatabaseLog
{
private $store = 'yonna_log';
private $config = null;
/**
* check yonna/database
* DatabaseLog constructor.
*/
public function __construct()
{
if (!class_exists(DB::class)) {
trigger_error('If you want to use database log,install composer package yonna/database please.');
return;
}
if (Config::getDatabase() === null) {
trigger_error('Set Database for DatabaseLog.');
return;
}
$this->config = Config::getDatabase();
}
/**
* 清除日志
*/
private function clear()
{
if (Config::getFileExpireDay() <= 0) {
return;
}
}
/**
* 分页获得数据
* @param array $options
* @return array
*/
public function page($options = [])
{
$current = $options['current'] ?? 1;
$per = $options['per'] ?? 10;
$res = [];
try {
$db = DB::connect($this->config);
if ($db instanceof Mongo) {
$obj = $db->collection("{$this->store}");
} elseif ($db instanceof Mysql) {
$obj = $db->table($this->store);
} elseif ($db instanceof Pgsql) {
$obj = $db->schemas('public')->table($this->store);
} else {
throw new Exception('Set Database for Support Driver.');
}
$obj = $obj->orderBy('log_time', 'desc');
if (!empty($options['key'])) {
$obj = $obj->equalTo('key', $options['key']);
}
if (!empty($options['type'])) {
$obj = $obj->equalTo('type', $options['key']);
}
if (!empty($options['log_time'])) {
$obj = $obj->between('log_time', $options['log_time']);
}
$res = $obj->page($current, $per);
} catch (Throwable $e) {
Log::file()->throwable($e, 'log_db');
}
return $res;
}
/**
* 写入日志
* @param $type
* @param array $data
* @param string $key
*/
private function append($type, $key, array $data = [])
{
if (empty($key) && empty($data)) {
return;
}
$db = DB::connect($this->config);
$logData = [
'key' => $key,
'type' => $type,
'log_time' => time(),
'data' => $data,
];
try {
if ($db instanceof Mongo) {
$db->collection($this->store)->insert($logData);
} elseif ($db instanceof Mysql) {
$db->query("CREATE TABLE IF NOT EXISTS `{$this->store}`(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
`key` char(255) NOT NULL DEFAULT 'default' COMMENT 'key',
`type` char(255) NOT NULL DEFAULT 'info' COMMENT '类型',
`log_time` int NOT NULL COMMENT '时间戳',
`data` json COMMENT 'data',
PRIMARY KEY (`id`)
) ENGINE = INNODB COMMENT 'log by yonna';");
$db->table($this->store)->insert($logData);
} elseif ($db instanceof Pgsql) {
$db->query("CREATE TABLE IF NOT EXISTS `{$this->store}`(
`id` bigserial NOT NULL,
`key` text NOT NULL DEFAULT 'default',
`type` text NOT NULL DEFAULT 'info',
`log_time` integer NOT NULL,
`data` jsonb,
PRIMARY KEY (`id`)
) ENGINE = INNODB COMMENT 'log by yonna';");
$db->schemas('public')->table($this->store)->insert($logData);
} else {
throw new Exception('Set Database for Support Driver.');
}
} catch (Throwable $e) {
Log::file()->throwable($e);
}
$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);
}
}