-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
LogServer.php
102 lines (92 loc) · 2.77 KB
/
LogServer.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
<?php
declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/SForum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/SForum/blob/master/LICENSE
*/
namespace App\CodeFec\Admin;
use Hyperf\Stringable\Str;
use SleekDB\Exceptions\IdNotAllowedException;
use SleekDB\Exceptions\InvalidArgumentException;
use SleekDB\Exceptions\InvalidConfigurationException;
use SleekDB\Exceptions\IOException;
use SleekDB\Exceptions\JsonException;
use SleekDB\Query;
use SleekDB\Store;
/**
* 日志服务
*/
class LogServer
{
private string $store;
private string $name;
private string $log;
private string | array | object $data;
private array $db_config = [
'auto_cache' => true,
'cache_lifetime' => null,
'timeout' => 120, // deprecated! Set it to false!
'primary_key' => '_id',
'search' => [
'min_length' => 2,
'mode' => 'or',
'score_key' => 'scoreKey',
'algorithm' => Query::SEARCH_ALGORITHM['hits'],
],
'folder_permissions' => 0777,
];
/**
* nosql.
* @throws \SleekDB\Exceptions\IOException
* @throws \SleekDB\Exceptions\InvalidArgumentException
* @throws \SleekDB\Exceptions\InvalidConfigurationException
*/
public function db(): Store
{
if (! is_dir(BASE_PATH . '/runtime/logs/admin_logger_database')) {
mkdir(BASE_PATH . '/runtime/logs/admin_logger_database');
}
$databasePath = BASE_PATH . '/runtime/logs/admin_logger_database/' . date('YmW');
return @new Store('logger', $databasePath, $this->db_config);
}
/**
* 插入日志数据.
* @param $store
* @param $name
* @param $log
* @param $data
* @throws IOException
* @throws IdNotAllowedException
* @throws InvalidArgumentException
* @throws InvalidConfigurationException
* @throws JsonException
* @return array
*/
public function insert($store, $name, $log, $data)
{
$this->store = $store;
$this->name = $name;
$this->log = $log;
$this->data = $data;
return $this->db()->insert([
'store' => $this->store,
'name' => $this->name,
'log' => $this->log,
'data' => $this->data,
'_token' => Str::random(),
'created_at' => date('Y-m-d H:i:s'),
]);
}
/**
* 获取所有数据.
* @throws \SleekDB\Exceptions\IOException
* @throws \SleekDB\Exceptions\InvalidArgumentException
*/
public function get(): array
{
return $this->db()->createQueryBuilder()->orderBy(['created_at' => 'desc', 'id' => 'desc'])->getQuery()->fetch();
}
}