1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Log configure |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 21/11/2017 |
6
|
|
|
* Time: 6:18 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Log; |
10
|
|
|
|
11
|
|
|
use Carno\Config\Config; |
12
|
|
|
use Carno\Config\Features\Overrider; |
13
|
|
|
use Carno\Log\Contracts\Formatter; |
14
|
|
|
use Carno\Log\Contracts\Outputter; |
15
|
|
|
use Carno\Log\Contracts\Replicated; |
16
|
|
|
use Carno\Log\Formatter\JSON; |
17
|
|
|
use Carno\Log\Formatter\Text; |
18
|
|
|
use Carno\Log\Outputter\Stdout; |
19
|
|
|
use Carno\Log\Outputter\TCP; |
20
|
|
|
use Carno\Log\Replicator\LogIO; |
21
|
|
|
use Carno\Net\Address; |
22
|
|
|
use Psr\Log\LogLevel; |
23
|
|
|
use Closure; |
24
|
|
|
|
25
|
|
|
class Configure |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Config |
29
|
|
|
*/ |
30
|
|
|
private $cfg = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Environment |
34
|
|
|
*/ |
35
|
|
|
private $env = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Connections |
39
|
|
|
*/ |
40
|
|
|
private $cmg = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Overrider[] |
44
|
|
|
*/ |
45
|
|
|
private $watched = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Configure constructor. |
49
|
|
|
* @param Config $cfg |
50
|
|
|
* @param Environment $env |
51
|
|
|
* @param Connections $cmg |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Config $cfg, Environment $env, Connections $cmg = null) |
54
|
|
|
{ |
55
|
|
|
$this->cfg = $cfg; |
56
|
|
|
$this->env = $env; |
57
|
|
|
$this->cmg = $cmg; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $scene |
62
|
|
|
* @param Closure $sync |
63
|
|
|
*/ |
64
|
|
|
public function syncLevel(string $scene, Closure $sync) : void |
65
|
|
|
{ |
66
|
|
|
$sync(debug() ? LogLevel::DEBUG : LogLevel::INFO); |
67
|
|
|
|
68
|
|
|
$this->watched[] = $this->cfg->overrides(static function (string $level) use ($sync) { |
69
|
|
|
debug() || $sync($level); |
70
|
|
|
}, 'log.level', $scene . '.log.level'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $scene |
75
|
|
|
* @param Closure $sync |
76
|
|
|
*/ |
77
|
|
|
public function syncFormatter(string $scene, Closure $sync) : void |
78
|
|
|
{ |
79
|
|
|
$sync($this->getFormatter('text')); |
80
|
|
|
|
81
|
|
|
$this->watched[] = $this->cfg->overrides(function (string $type) use ($sync) { |
82
|
|
|
debug() || $sync($this->getFormatter($type)); |
83
|
|
|
}, 'log.format', $scene . '.log.format'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $scene |
88
|
|
|
* @param Closure $sync |
89
|
|
|
*/ |
90
|
|
|
public function syncOutputter(string $scene, Closure $sync) : void |
91
|
|
|
{ |
92
|
|
|
$sync($this->getOutputter('stdout://')); |
93
|
|
|
|
94
|
|
|
$this->watched[] = $this->cfg->overrides(function (string $dsn) use ($sync) { |
95
|
|
|
debug() || $sync($this->getOutputter($dsn)); |
96
|
|
|
}, 'log.addr', $scene . '.log.addr'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $scene |
101
|
|
|
* @param Closure $sync |
102
|
|
|
*/ |
103
|
|
|
public function syncReplicator(string $scene, Closure $sync) : void |
104
|
|
|
{ |
105
|
|
|
$this->watched[] = $this->cfg->overrides(function (string $dsn = null) use ($sync) { |
106
|
|
|
debug() || $sync($this->getReplicator($dsn)); |
107
|
|
|
}, 'log.replica', $scene . '.log.replica'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
*/ |
112
|
|
|
public function unload() : void |
113
|
|
|
{ |
114
|
|
|
foreach ($this->watched as $watcher) { |
115
|
|
|
$watcher->unwatch(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $type |
121
|
|
|
* @return Formatter |
122
|
|
|
*/ |
123
|
|
|
private function getFormatter(string $type) : Formatter |
124
|
|
|
{ |
125
|
|
|
switch ($type) { |
126
|
|
|
case 'json': |
127
|
|
|
return new JSON($this->env); |
128
|
|
|
default: |
129
|
|
|
return new Text(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $dsn |
135
|
|
|
* @return Outputter |
136
|
|
|
*/ |
137
|
|
|
private function getOutputter(string $dsn) : Outputter |
138
|
|
|
{ |
139
|
|
|
$parsed = parse_url($dsn); |
140
|
|
|
|
141
|
|
|
switch ($parsed['scheme'] ?? 'default') { |
142
|
|
|
case 'tcp': |
143
|
|
|
if ($this->cmg) { |
144
|
|
|
return $this->cmg->hosting( |
145
|
|
|
new Address($parsed['host'], $parsed['port'] ?? 80), |
146
|
|
|
static function (Address $address) { |
147
|
|
|
return new TCP($address); |
148
|
|
|
} |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return new Stdout(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $dsn |
158
|
|
|
* @return Replicated |
159
|
|
|
*/ |
160
|
|
|
private function getReplicator(string $dsn = null) : ?Replicated |
161
|
|
|
{ |
162
|
|
|
$parsed = parse_url($dsn); |
163
|
|
|
|
164
|
|
|
switch ($parsed['scheme'] ?? 'default') { |
165
|
|
|
case 'logio': |
166
|
|
|
if ($this->cmg) { |
167
|
|
|
return new LogIO($this->env, $this->cmg, new Address($parsed['host'], $parsed['port'] ?? 28777)); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return null; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|