bytic /
logger
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Nip\Logger\Manager; |
||||||
| 4 | |||||||
| 5 | use League\Container\Exception\NotFoundException; |
||||||
| 6 | use Monolog\Formatter\LineFormatter; |
||||||
| 7 | use Monolog\Handler\FormattableHandlerInterface; |
||||||
| 8 | use Monolog\Handler\HandlerInterface; |
||||||
| 9 | use Monolog\Logger as Monolog; |
||||||
| 10 | use Nip\Config\Config; |
||||||
| 11 | use Nip\Container\Container; |
||||||
| 12 | use Psr\Log\InvalidArgumentException; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * Class MonologWrappers |
||||||
| 16 | * @package Nip\Logger\Manager |
||||||
| 17 | * |
||||||
| 18 | * @method Container getContainer() |
||||||
| 19 | */ |
||||||
| 20 | trait MonologWrappers |
||||||
| 21 | { |
||||||
| 22 | /** |
||||||
| 23 | * Create an instance of any handler available in Monolog. |
||||||
| 24 | * |
||||||
| 25 | * @param array $config |
||||||
| 26 | 2 | * @return \Psr\Log\LoggerInterface |
|||||
| 27 | * |
||||||
| 28 | 2 | * @throws \InvalidArgumentException |
|||||
| 29 | */ |
||||||
| 30 | 2 | protected function createMonologDriver($config) |
|||||
| 31 | { |
||||||
| 32 | $config = $config instanceof Config ? $config->toArray() : $config; |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 33 | |||||||
| 34 | if (!is_a($config['handler'], HandlerInterface::class, true)) { |
||||||
| 35 | throw new InvalidArgumentException( |
||||||
| 36 | 2 | $config['handler'].' must be an instance of '.HandlerInterface::class |
|||||
| 37 | 2 | ); |
|||||
| 38 | 2 | } |
|||||
| 39 | 2 | ||||||
| 40 | $with = array_merge( |
||||||
| 41 | ['level' => $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...
|
|||||||
| 42 | 2 | $config['with'] ?? [], |
|||||
| 43 | 2 | $config['handler_with'] ?? [] |
|||||
| 44 | 2 | ); |
|||||
| 45 | 2 | ||||||
| 46 | $handler = $this->getContainer()->make($config['handler'], $with); |
||||||
| 47 | |||||||
| 48 | return new Monolog( |
||||||
| 49 | $this->parseChannel($config), |
||||||
|
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...
|
|||||||
| 50 | [$this->prepareHandler($handler, $config)] |
||||||
| 51 | ); |
||||||
| 52 | } |
||||||
| 53 | |||||||
| 54 | /** |
||||||
| 55 | * Prepare the handlers for usage by Monolog. |
||||||
| 56 | * |
||||||
| 57 | * @param array $handlers |
||||||
| 58 | * @return array |
||||||
| 59 | */ |
||||||
| 60 | protected function prepareHandlers(array $handlers) |
||||||
| 61 | { |
||||||
| 62 | foreach ($handlers as $key => $handler) { |
||||||
| 63 | $handlers[$key] = $this->prepareHandler($handler); |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | return $handlers; |
||||||
| 67 | } |
||||||
| 68 | |||||||
| 69 | /** |
||||||
| 70 | * Prepare the handler for usage by Monolog. |
||||||
| 71 | * |
||||||
| 72 | 4 | * @param \Monolog\Handler\HandlerInterface $handler |
|||||
| 73 | * @param array $config |
||||||
| 74 | 4 | * @return \Monolog\Handler\HandlerInterface |
|||||
| 75 | */ |
||||||
| 76 | 4 | protected function prepareHandler(HandlerInterface $handler, array $config = []) |
|||||
| 77 | { |
||||||
| 78 | 4 | $isHandlerFormattable = false; |
|||||
| 79 | 4 | ||||||
| 80 | if (Monolog::API === 1) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 81 | $isHandlerFormattable = true; |
||||||
| 82 | 4 | } elseif (Monolog::API === 2 && $handler instanceof FormattableHandlerInterface) { |
|||||
| 83 | 4 | $isHandlerFormattable = true; |
|||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | if ($isHandlerFormattable && !isset($config['formatter'])) { |
||||||
| 87 | $handler->setFormatter($this->formatter()); |
||||||
|
0 ignored issues
–
show
The method
setFormatter() does not exist on Monolog\Handler\HandlerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Monolog\Handler\Handler or Monolog\Handler\AbstractHandler or Monolog\Handler\NullHandler or Monolog\Handler\NoopHandler. Are you sure you never get one of those?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 88 | 4 | } elseif ($isHandlerFormattable && $config['formatter'] !== 'default') { |
|||||
| 89 | $handler->setFormatter($this->app->make($config['formatter'], $config['formatter_with'] ?? [])); |
||||||
| 90 | } |
||||||
| 91 | |||||||
| 92 | return $handler; |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | /** |
||||||
| 96 | 4 | * Get a Monolog formatter instance. |
|||||
| 97 | * |
||||||
| 98 | 4 | * @return \Monolog\Formatter\FormatterInterface |
|||||
| 99 | 4 | */ |
|||||
| 100 | protected function formatter() |
||||||
| 101 | 4 | { |
|||||
| 102 | $formatter = new LineFormatter(null, $this->dateFormat, true, true); |
||||||
| 103 | $formatter->includeStacktraces(); |
||||||
| 104 | |||||||
| 105 | return $formatter; |
||||||
| 106 | } |
||||||
| 107 | } |
||||||
| 108 |