|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Seasx\SeasLogger; |
|
5
|
|
|
|
|
6
|
|
|
use Seasx\SeasLogger\targets\AbstractTarget; |
|
7
|
|
|
use Swoole\Timer; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Interface ConfigInterface |
|
11
|
|
|
* @package Seasx\SeasLogger |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractConfig |
|
14
|
|
|
{ |
|
15
|
|
|
const TYPE_JSON = 'json'; |
|
16
|
|
|
const TYPE_FIELD = 'field'; |
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
protected static $supportField = [ |
|
19
|
|
|
self::TYPE_JSON, |
|
20
|
|
|
self::TYPE_FIELD |
|
21
|
|
|
]; |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
protected $split = ' | '; |
|
24
|
|
|
/** @var int */ |
|
25
|
|
|
protected $bufferSize = 1; |
|
26
|
|
|
/** @var AbstractTarget[] */ |
|
27
|
|
|
protected $targetList = []; |
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
protected $tick = 0; |
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
protected $recall_depth = 0; |
|
32
|
|
|
/** @var callable */ |
|
33
|
|
|
protected $userTemplate; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $appName = 'Seaslog'; |
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
protected $template; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
protected $customerType = self::TYPE_FIELD; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* AbstractConfig constructor. |
|
43
|
|
|
* @param array $target |
|
44
|
|
|
* @param array $configs |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $target, array $configs = []) |
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($configs as $name => $value) { |
|
49
|
|
|
if (property_exists($this, $name) && $name !== 'targetList') { |
|
50
|
|
|
$this->$name = $value; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
$this->targetList = $target; |
|
54
|
|
|
foreach ($this->targetList as $target) { |
|
55
|
|
|
$target->setTemplate($this->template)->setCustomerFieldType($this->customerType)->setSplit($this->split); |
|
56
|
|
|
} |
|
57
|
|
|
register_shutdown_function(function () { |
|
58
|
|
|
$this->flush(true); |
|
59
|
|
|
}); |
|
60
|
|
|
$this->tick > 0 && Timer::tick($this->tick * 1000, [$this, 'flush'], [true]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getTemplate(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->template; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param bool $flush |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract public function flush(bool $flush = false): void; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
abstract public function getBuffer(): array; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract public function getDatetimeFormat(): string; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $format |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function setDatetimeFormat(string $format): bool; |
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getSupportFieldType(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return static::$supportField; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param callable $userTemplate |
|
101
|
|
|
*/ |
|
102
|
|
|
public function registerTemplate(callable $userTemplate): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->userTemplate = $userTemplate; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $level |
|
109
|
|
|
* @param string $message |
|
110
|
|
|
* @param array $context |
|
111
|
|
|
*/ |
|
112
|
|
|
abstract public function log(string $level, string $message, array $context = []): void; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function getUserTemplate(): array |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->userTemplate) { |
|
120
|
|
|
$template = call_user_func($this->userTemplate); |
|
121
|
|
|
$template = $template ?? []; |
|
122
|
|
|
} else { |
|
123
|
|
|
$template = []; |
|
124
|
|
|
} |
|
125
|
|
|
return $template; |
|
126
|
|
|
} |
|
127
|
|
|
} |