1 | <?php |
||||||
2 | |||||||
3 | namespace Nip\Logger\Manager; |
||||||
4 | |||||||
5 | use Monolog\Handler\HandlerInterface; |
||||||
6 | use Monolog\Handler\RotatingFileHandler; |
||||||
7 | use Monolog\Handler\StreamHandler; |
||||||
8 | use Monolog\Logger as Monolog; |
||||||
9 | use Nip\Config\Config; |
||||||
10 | use Nip\Logger\Logger; |
||||||
11 | use Psr\Log\InvalidArgumentException; |
||||||
12 | use Psr\Log\LoggerInterface; |
||||||
13 | |||||||
14 | /** |
||||||
15 | * Trait CreateDrivers |
||||||
16 | * @package Nip\Logger\Traits |
||||||
17 | */ |
||||||
18 | trait CreateDrivers |
||||||
19 | { |
||||||
20 | |||||||
21 | /** |
||||||
22 | * The registered custom driver creators. |
||||||
23 | * |
||||||
24 | * @var array |
||||||
25 | */ |
||||||
26 | protected $customCreators = []; |
||||||
27 | |||||||
28 | /** |
||||||
29 | * @param $name |
||||||
30 | * @return mixed |
||||||
31 | */ |
||||||
32 | 4 | protected function initDriver($name) |
|||||
33 | { |
||||||
34 | 4 | $this->channels[$name] = $this->createLogger($this->resolve($name)); |
|||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
35 | |||||||
36 | 4 | return $this->channels[$name]; |
|||||
37 | } |
||||||
38 | |||||||
39 | /** |
||||||
40 | * Resolve the given log instance by name. |
||||||
41 | * |
||||||
42 | * @param string $name |
||||||
43 | * @return LoggerInterface |
||||||
44 | * |
||||||
45 | * @throws \InvalidArgumentException |
||||||
46 | */ |
||||||
47 | 4 | protected function resolve($name) |
|||||
48 | { |
||||||
49 | 4 | $config = $this->configurationFor($name); |
|||||
50 | |||||||
51 | 4 | if (is_null($config)) { |
|||||
0 ignored issues
–
show
|
|||||||
52 | throw new InvalidArgumentException("Log [{$name}] is not defined."); |
||||||
53 | } |
||||||
54 | |||||||
55 | 4 | if (isset($this->customCreators[$config['driver']])) { |
|||||
56 | return $this->callCustomCreator($config); |
||||||
57 | } |
||||||
58 | |||||||
59 | 4 | $driverMethod = 'create' . ucfirst($config['driver']) . 'Driver'; |
|||||
60 | |||||||
61 | 4 | if (method_exists($this, $driverMethod)) { |
|||||
62 | 4 | return $this->{$driverMethod}($config); |
|||||
63 | } |
||||||
64 | |||||||
65 | throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); |
||||||
66 | } |
||||||
67 | |||||||
68 | /** |
||||||
69 | * Call a custom driver creator. |
||||||
70 | * |
||||||
71 | * @param array $config |
||||||
72 | * @return mixed |
||||||
73 | */ |
||||||
74 | protected function callCustomCreator(array $config) |
||||||
75 | { |
||||||
76 | return $this->customCreators[$config['driver']](app(), $config); |
||||||
77 | } |
||||||
78 | |||||||
79 | /** |
||||||
80 | * Create an aggregate log driver instance. |
||||||
81 | * |
||||||
82 | * @param array $config |
||||||
83 | * @return LoggerInterface |
||||||
84 | */ |
||||||
85 | 1 | protected function createStackDriver($config) |
|||||
86 | { |
||||||
87 | 1 | $config = $config instanceof Config ? $config->toArray() : $config; |
|||||
0 ignored issues
–
show
|
|||||||
88 | |||||||
89 | 1 | $handlers = []; |
|||||
90 | array_map(function ($channel) use (&$handlers) { |
||||||
91 | 1 | $handlers = array_merge($handlers, $this->channel($channel)->getHandlers()); |
|||||
0 ignored issues
–
show
It seems like
channel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
92 | 1 | }, $config['channels']); |
|||||
93 | |||||||
94 | // if ($config['ignore_exceptions'] ?? false) { |
||||||
95 | // $handlers = [new WhatFailureGroupHandler($handlers)]; |
||||||
96 | // } |
||||||
97 | |||||||
98 | 1 | return new Monolog($this->parseChannel($config), $handlers); |
|||||
0 ignored issues
–
show
It seems like
parseChannel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
99 | } |
||||||
100 | |||||||
101 | /** |
||||||
102 | * Create an instance of the single file log driver. |
||||||
103 | * |
||||||
104 | * @param array $config |
||||||
105 | * @return LoggerInterface |
||||||
106 | */ |
||||||
107 | 1 | protected function createSingleDriver($config) |
|||||
108 | { |
||||||
109 | 1 | $config = $config instanceof Config ? $config->toArray() : $config; |
|||||
0 ignored issues
–
show
|
|||||||
110 | $handlers = [ |
||||||
111 | 1 | $this->prepareHandler( |
|||||
112 | 1 | new StreamHandler( |
|||||
113 | 1 | $config['path'] ?? $this->getLogsFolderPath() . '/bytic.log', |
|||||
0 ignored issues
–
show
It seems like
getLogsFolderPath() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
114 | 1 | $this->level($config), |
|||||
0 ignored issues
–
show
It seems like
level() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
115 | 1 | $config['bubble'] ?? true, |
|||||
116 | 1 | $config['permission'] ?? null, |
|||||
117 | 1 | $config['locking'] ?? false |
|||||
118 | ), |
||||||
119 | 1 | $config |
|||||
120 | ), |
||||||
121 | ]; |
||||||
122 | |||||||
123 | 1 | return new Monolog($this->parseChannel($config), $handlers); |
|||||
124 | } |
||||||
125 | |||||||
126 | /** |
||||||
127 | * @param array $config |
||||||
128 | * @return Monolog |
||||||
129 | */ |
||||||
130 | 1 | protected function createDailyDriver($config) |
|||||
131 | { |
||||||
132 | 1 | $config = $config instanceof Config ? $config->toArray() : $config; |
|||||
0 ignored issues
–
show
|
|||||||
133 | |||||||
134 | $handlers = [ |
||||||
135 | 1 | $this->prepareHandler(new RotatingFileHandler( |
|||||
136 | 1 | $config['path'], |
|||||
137 | 1 | $config['days'] ?? 7, |
|||||
138 | 1 | $this->level($config), |
|||||
139 | 1 | $config['bubble'] ?? true, |
|||||
140 | 1 | $config['permission'] ?? null, |
|||||
141 | 1 | $config['locking'] ?? false |
|||||
142 | 1 | ), $config), |
|||||
143 | ]; |
||||||
144 | |||||||
145 | 1 | return new Monolog($this->parseChannel($config), $handlers); |
|||||
146 | } |
||||||
147 | |||||||
148 | /** |
||||||
149 | * @param array $config |
||||||
150 | * @return Monolog |
||||||
151 | */ |
||||||
152 | protected function createNewrelicDriver($config) |
||||||
153 | { |
||||||
154 | $config = $config instanceof Config ? $config->toArray() : $config; |
||||||
0 ignored issues
–
show
|
|||||||
155 | $config['handler'] = isset($config['handler']) ? $config['handler'] : \ByTIC\NewRelic\Monolog\Handler::class; |
||||||
156 | |||||||
157 | return $this->createMonologDriver($config); |
||||||
0 ignored issues
–
show
It seems like
createMonologDriver() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
158 | } |
||||||
159 | |||||||
160 | /** |
||||||
161 | * Create an emergency log handler to avoid white screens of death. |
||||||
162 | * |
||||||
163 | * @return LoggerInterface |
||||||
164 | */ |
||||||
165 | protected function createEmergencyLogger() |
||||||
166 | { |
||||||
167 | $config = $this->configurationFor('emergency'); |
||||||
168 | |||||||
169 | $handler = new StreamHandler( |
||||||
170 | $config['path'] ?? $this->getLogsFolderPath() . '/bytic.log', |
||||||
171 | $this->level(['level' => 'debug']) |
||||||
172 | ); |
||||||
173 | |||||||
174 | return $this->createLogger( |
||||||
175 | new Monolog('bytic', [$this->prepareHandler($handler)]) |
||||||
176 | ); |
||||||
177 | } |
||||||
178 | |||||||
179 | /** |
||||||
180 | * @param $logger |
||||||
181 | * @return Logger|LoggerInterface |
||||||
182 | */ |
||||||
183 | abstract protected function createLogger($logger); |
||||||
184 | |||||||
185 | /** |
||||||
186 | * @param string $logger |
||||||
187 | * @return array |
||||||
188 | */ |
||||||
189 | abstract protected function configurationFor($logger); |
||||||
190 | |||||||
191 | /** |
||||||
192 | * @param HandlerInterface $handler |
||||||
193 | * @param array $config |
||||||
194 | * @return HandlerInterface |
||||||
195 | */ |
||||||
196 | abstract protected function prepareHandler(HandlerInterface $handler, array $config = []); |
||||||
197 | } |
||||||
198 |