1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
* |
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
13
|
|
|
* and is licensed under the MIT license. |
14
|
|
|
* |
15
|
|
|
* Copyright (c) 2015-2021 Yuuki Takezawa |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Ytake\LaravelFluent; |
21
|
|
|
|
22
|
|
|
use Fluent\Logger\FluentLogger; |
23
|
|
|
use Fluent\Logger\PackerInterface; |
24
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException; |
25
|
|
|
use Illuminate\Contracts\Container\Container; |
26
|
|
|
use Illuminate\Log\LogManager; |
27
|
|
|
use Monolog\Handler\HandlerInterface; |
28
|
|
|
use Monolog\Logger as Monolog; |
29
|
|
|
use Psr\Log\LoggerInterface; |
30
|
|
|
|
31
|
|
|
use function class_exists; |
32
|
|
|
use function is_null; |
33
|
|
|
use function strval; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* FluentLogManager |
37
|
|
|
*/ |
38
|
|
|
final class FluentLogManager extends LogManager |
39
|
|
|
{ |
40
|
|
|
/** @var Container */ |
41
|
|
|
protected $app; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array<string, mixed> $config |
45
|
|
|
* @return LoggerInterface |
46
|
|
|
* @throws BindingResolutionException |
47
|
|
|
*/ |
48
|
|
|
protected function createFluentDriver(array $config): LoggerInterface |
49
|
|
|
{ |
50
|
|
|
return new Monolog($this->parseChannel($config), [ |
51
|
|
|
$this->prepareHandler( |
52
|
|
|
$this->createFluentHandler($config) |
53
|
|
|
), |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array<string, mixed> $config |
59
|
|
|
* @return HandlerInterface |
60
|
|
|
* @throws BindingResolutionException |
61
|
|
|
*/ |
62
|
|
|
private function createFluentHandler(array $config): HandlerInterface |
63
|
|
|
{ |
64
|
|
|
$configure = $this->app->make('config')['fluent']; |
65
|
|
|
$fluentHandler = $this->detectHandler($configure); |
66
|
|
|
|
67
|
|
|
$handler = new $fluentHandler( |
68
|
|
|
new FluentLogger( |
69
|
|
|
$configure['host'] ?? FluentLogger::DEFAULT_ADDRESS, |
70
|
|
|
(int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT, |
71
|
|
|
$configure['options'] ?? [], |
72
|
|
|
$this->detectPacker($configure) |
73
|
|
|
), |
74
|
|
|
$configure['tagFormat'] ?? null, |
75
|
|
|
$this->level($config) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
if (isset($configure['processors']) && is_array($configure['processors'])) { |
79
|
|
|
foreach ($configure['processors'] as $processor) { |
80
|
|
|
if (is_string($processor) && class_exists($processor)) { |
81
|
|
|
$processor = $this->app->make($processor); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$handler->pushProcessor($processor); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $handler; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array<string, mixed> $config |
93
|
|
|
* |
94
|
|
|
* @return LoggerInterface |
95
|
|
|
* @throws BindingResolutionException |
96
|
|
|
*/ |
97
|
|
|
public function __invoke(array $config): LoggerInterface |
98
|
|
|
{ |
99
|
|
|
return $this->createFluentDriver($config); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function defaultHandler(): string |
106
|
|
|
{ |
107
|
|
|
return FluentHandler::class; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array<string, mixed> $configure |
112
|
|
|
* |
113
|
|
|
* @return PackerInterface|null |
114
|
|
|
* @throws BindingResolutionException |
115
|
|
|
*/ |
116
|
|
|
protected function detectPacker(array $configure): ?PackerInterface |
117
|
|
|
{ |
118
|
|
|
if (!is_null($configure['packer'])) { |
119
|
|
|
if (class_exists($configure['packer'])) { |
120
|
|
|
return $this->app->make($configure['packer']); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array<string, mixed> $configure |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function detectHandler(array $configure): string |
132
|
|
|
{ |
133
|
|
|
$handler = $configure['handler'] ?? null; |
134
|
|
|
if (!is_null($handler)) { |
135
|
|
|
if (class_exists($handler)) { |
136
|
|
|
return strval($handler); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return $this->defaultHandler(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|